[web2py] Re: Routing help

2012-05-18 Thread Alexander McLin
Sorry I posted too early,

My solution is:

in routes.py in the base folder, where myapp is the actual name of the 
application I'm developing.

routes_app = ((r'/(?Pwelcome|admin|myapp)\b.*', r'\g'),)

and in myapp's folder, I have another routes.py with the following,

routes_in = ((r'/myapp/users/(?P\d*)', r'/myapp/users/index/\g'),)

routes_out = ((r'/myapp/users/index/(?P\d*)', r'/myapp/users/\g'),')

This is what worked for me. 


Alex


On Friday, May 18, 2012 2:11:28 PM UTC-4, Alexander McLin wrote:
>
> Sorry if I wasn't clear, I actually meant URLs of the following form, 
> myapp/controller/args to be mapped to myapp/controller/index/args. The 
> second point of confusion takes a different tack on web2py routing than the 
> first point.
>
> I'll try to experiment with your solution though.
>
> For future notes, I actually got the pattern-matching solution working, I 
> realized that what I was missing was that I had to include my application's 
> name in the routes_app for the custom routes to be enabled.
>
> In any event, my solution is
>
> On Friday, May 18, 2012 9:17:30 AM UTC-4, Wikus van de Merwe wrote:
>>
>> OK, so you want /myapp/args to be mapped to 
>> /myapp/default_controller/default_function/args. By default args would be 
>> interpreted as a function name and will be mapped to 
>> /myapp/default_controller/args. To change that you need to define a list of 
>> functions for the default controller. Then if args is not in that list it 
>> would be mapped to /myapp/default_controller/default_function/args.
>>
>> routers = dict( 
>> myapp = dict(
>> default_controller = "default",
>> default_function = "index",
>> functions = ["fun1", "fun2", "fun3"]
>> )
>> )
>>
>>

[web2py] Re: Routing help

2012-05-18 Thread Alexander McLin
Sorry if I wasn't clear, I actually meant URLs of the following form, 
myapp/controller/args to be mapped to myapp/controller/index/args. The 
second point of confusion takes a different tack on web2py routing than the 
first point.

I'll try to experiment with your solution though.

For future notes, I actually got the pattern-matching solution working, I 
realized that what I was missing was that I had to include my application's 
name in the routes_app for the custom routes to be enabled.

In any event, my solution is

On Friday, May 18, 2012 9:17:30 AM UTC-4, Wikus van de Merwe wrote:
>
> OK, so you want /myapp/args to be mapped to 
> /myapp/default_controller/default_function/args. By default args would be 
> interpreted as a function name and will be mapped to 
> /myapp/default_controller/args. To change that you need to define a list of 
> functions for the default controller. Then if args is not in that list it 
> would be mapped to /myapp/default_controller/default_function/args.
>
> routers = dict( 
> myapp = dict(
> default_controller = "default",
> default_function = "index",
> functions = ["fun1", "fun2", "fun3"]
> )
> )
>
>

[web2py] Re: Routing help

2012-05-18 Thread Alexander McLin
Thank you for the clarification, I understand it better now. I was indeed 
confused by the statement you quoted, I had thought that web2py's base 
behavior would that if it couldn't find a controller corresponding to 
somefunction, it would then invoke the default controller and search for 
somefunction on it which then successfully executes. Of course if the 
default controller doesn't have somefunction then web2py's final action 
would be to return an error.

Alex

On Thursday, May 17, 2012 5:57:54 PM UTC-4, Anthony wrote:
>
> The web2py book and documentation has led me to believe that the following 
>> would work;
>>
>> http://120.0.0.1/myapp/default/somefunction can be written as 
>> http://120.0.0.1/myapp/somefunction but I get invalid request error
>>
>
> Here's what the book says:
>
> web2py maps a URL of the form:
>
> 1.
>
> http://127.0.0.1:8000/a/c/f.html
>
> to the function f() in controller "c.py" in application "a". If f is not 
> present, web2py defaults to the indexcontroller function. If c is not 
> present, web2py defaults to the "default.py" controller, and if a is not 
> present, web2py defaults to the init application. If there is no init 
> application, 
> web2py tries to run the welcomeapplication.
>
> I think I see the confusion. It says if the controller is not present, 
> web2py defaults to the default.py controller. However, when you have the 
> URL /myapp/somefunction, technically the controller is present, as web2py 
> will interpret the second element of the URL as the controller (just 
> because you know somefunction is a function doesn't mean the default router 
> does) -- in that case, it's actually the function that is missing, not the 
> controller. So, what the above really means is:
>
> /myapp maps to /myapp/default/index
> /myapp/default maps to /myapp/default/index
>
> but /myapp/somefunction does not map to /myapp/default/somefunction 
> because somefunction is interpreted as a controller in that URL (instead, 
> it maps to /myapp/somefunction/index, and you get an error because such a 
> controller doesn't exist).
>  
>
>> If I then overwrite routes.py using routes.example.py, the above case no 
>> longer works. Looking at both routes.example.py and router.example.py, 
>> it looks like both of them configure web2py's routing behavior in the same 
>> way, but the actual behavior is not the same.
>>
>
> I assume you're referring to the default_controller='default' parameter 
> set in the routes.example.py example. I agree that's a little confusing 
> because you might expect it to work much like setting the default 
> controller in the parameter-based system, but again, let's look at what the 
> book says:
>
> When using the pattern-based system, the name of the default application, 
> controller, and function can be changed from *init*, *default*, and *index
> * respectively to another name by setting the appropriate value in 
> routes.py 
>
>
> That's saying that setting the value of default_controller merely changes 
> the default value used by the standard routing system when that item is 
> missing from the URL. So, let's say you set 
> default_controller='mycontroller'. In that case, /myapp would map to 
> /myapp/mycontroller/index instead of /myapp/default/index, but 
> /myapp/somefunction would still cause a problem because somefunction would 
> be seen as a controller.
>
> One thing to note is that the parameter-based system was created quite a 
> while after the pattern-based system, so seemingly similar functionality 
> wasn't necessarily designed to behave the same way (hopefully the behavior 
> of the parameter-based system is seen as an improvement in this case).
>
> Anyway, I agree some of this could be better explained in the book.
>
> Anthony
>
>

[web2py] Routing help

2012-05-17 Thread Alexander McLin
Hello,

I'm new to web2py and am having a difficult time understanding how to use 
either the parameter or pattern based systems as well how web2py default 
routing works.

*I'll start off with the first point of confusion for me;*

The web2py book and documentation has led me to believe that the following 
would work;

http://120.0.0.1/myapp/default/somefunction can be written as 
http://120.0.0.1/myapp/somefunction but I get invalid request error

If I copy router.example.py to routes.py in the base directory, the above 
case then works as I expected, but I thought that even with a lack of a 
routes.py file, web2py would still automatically default to the same 
behavior. This isn't so at all?

If I then overwrite routes.py using routes.example.py, the above case no 
longer works. Looking at both routes.example.py and router.example.py, it 
looks like both of them configure web2py's routing behavior in the same 
way, but the actual behavior is not the same.

So I'm pretty confused about this. Note, after each changes to routes.py I 
do reload the routes in web2py's admin console.

*The second point of confusion for me is;*
*
*
In the application I'm writing, which uses AJAX heavily and uses PUT and 
DELETE in addition to GET and POST.

I have in one of the controller, users, the following;

def index():
def GET():
   blah
def POST(*vars, **fields):
  blah
def PUT(*vars, **fields):
  blah
def DELETE(*vars, **fields):
  blah

I want to map a PUT request to users/5 to users/index/5

Right now the current default routing configuration returns users/5 as 
invalid request which is expected. Unfortunately I still don't understand 
web2py well enough to know how to set up one of the routing systems to map 
users/ to users/index/

I think I need to use the pattern matching system but I'm not able to get 
it working properly.

Would appreciate your tips.

Thanks
Alex