You could also use the pattern-based rewrite system to route to entirely 
separate functions depending on the content requested (e.g., separate 
functions for "description" and "comments").

Anthony

On Tuesday, February 26, 2013 3:06:08 PM UTC-5, Anthony wrote:
>
> Note, you generally will not have to test for the length of request.args. 
> Instead, you can do request.args(...), and it will return None if the index 
> is out of range. So, your code could like something like this:
>
> def products():
>     product, content, comment = request.args(0), request.args(1), request.
> args(2)
>     if content == 'description':
>         # return description of requested product
>     elif content == 'comments':
>         if comment:
>             # return specific comment requested
>         else:
>             # return all comments of requested product
>     else:
>         if not product:
>              # redirect to error
>         # return all info on requested product
>
> Presumably the code would look similar in Django, except for the very 
> first line. Note, when there is only one arg (i.e., the product ID), then 
> "content" and "comment" will simply be None.
>
> You might also look into the URL rewrite 
> systems<http://web2py.com/books/default/chapter/29/04#URL-rewrite>as well as 
> the REST 
> API 
> functionality<http://web2py.com/books/default/chapter/29/10#Restful-Web-Services>
> .
>
> Anthony
>
> On Tuesday, February 26, 2013 2:43:59 PM UTC-5, [email protected] wrote:
>>
>> So, the way to handle these types of links (with the example before):
>>
>> welcome/default/products/(\d{2})     # shows overall product page for 
>> given product primary id
>> welcome/default/products/(\d{2})/description   # shows product 
>> description for given product primary id
>> welcome/default/products/(\d{2})/comments   # shows product comments for 
>> given product primary id
>> welcome/default/products/(\d{2})/comments/(\d{2})   # shows specific 
>> comment for a specific product given comment id and product id
>>
>> Would be to manually check request.args to determine what action to take? 
>> I imagine it would be kind of messy like this:
>>
>> def products():
>>     request_length = len(request.args)
>>
>>     if request_length == 1:
>>            # return all info on requested product
>>     elif request_length == 2:
>>            if request.args[1] == 'description':
>>                    # return description of requested product
>>            elif request.args[1] == 'comments':
>>                    # return comments of requested product
>>      elif request_length == 3:
>>             if request.args[1] == 'comments':
>>                   # return single comment given its id
>>      else:
>>              # redirect to error           
>>
>> I feel like I'm missing something because this seems a bit 
>> counter-intuitive and highly messy, especially as the number of urls may 
>> grow in a large site?
>>
>>
>>
>> On Tuesday, February 26, 2013 2:13:29 PM UTC-5, Anthony wrote:
>>>
>>> In that case, you could do something like:
>>>
>>> def places():
>>>     lastname, firstname = request.args[0:2]
>>>
>>> Then for a URL like /myapp/mycontroller/places/John/Doe, "John" would be 
>>> in request.args[0] and "Doe" would be in request.args[1].
>>>
>>> Anthony
>>>
>>> On Tuesday, February 26, 2013 11:57:55 AM UTC-5, [email protected]:
>>>>
>>>> Sorry, let me clarify and be more specific again. The framework I 
>>>> referred to was Django, where their url dispatcher can create named groups 
>>>> like so:
>>>>
>>>> r'^places/(?P<lastname>\w+)/(?P<firstname>\w+)/$', 'misc.views.home'
>>>>
>>>>
>>>> This url would be mapped to a function with the name given in the url:
>>>>
>>>>
>>>> def home(request, lastname, firstname)
>>>>
>>>>    # Do something with name and return data to webpage
>>>>
>>>>
>>>>
>>>> I don't quite understand how web2py's routes.py would handle this.
>>>>
>>>>
>>>>
>>>>
>>>>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to