Friday, June 15, 2012

Re: trailing slash defeats get_absolute_url

On Friday, June 15, 2012 9:42:33 AM UTC+2, Mike Dewhirst wrote:
On 15/06/2012 5:11pm, bruno desthuilliers wrote:
> You have to end your url patterns with a slash, ie r'^search/'.

Ok. I added a trailing slash to the get_absolute_url method like so ...  
return 'xitem/%s/' % self.id ...


While we're at it: you should definitely use django.core.urlresolvers.reverse (same thing as the {% url %} templatetag) to reverse-build your urls, instead of hard-coding them (well, unless you really enjoy having to grep thru your whole codebase when your url scheme changes). As a matter of fact, you wouldn't have had to edit this part if you had used reverse() instead, just adding the final slash in the url pattern would have been enough.

 
and added an extra url pattern ...

urlpatterns = patterns('item.views',
     url(r'^search/$', 'search'),

Note that you may want to make the slash conditional, ie:

   r'^search/?$'

so it will match both "/search" and "/search/"


HTH

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/iN2ICAX2EjcJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate