trailing slash defeats get_absolute_url

95 views
Skip to first unread message

Mike Dewhirst

unread,
Jun 14, 2012, 11:41:22 PM6/14/12
to django...@googlegroups.com
My urls are working without a trailing slash but not with one. Django
version 1.5, Python 2.7.

settings.APPEND_SLASH = True (but there is no difference if it is False)

Here is the working part ...

http://127.0.0.1:8000/item/search

When this search retrieves a list of xitems the urls to display them are
correct like this ...

http://127.0.0.1:8000/item/xitem/3

... and displays nicely.

Here is the failing one ...

http://127.0.0.1:8000/item/search/

When this search with a trailing slash retrieves the same list the hrefs
are incorrect because they include the 'search/' fragment like this ...

http://127.0.0.1:8000/item/search/xitem/3

Here is my urls.py which sits in the item directory ...

urlpatterns = patterns('item.views',
url(r'^search', 'search'),
url(r'^xitem/(?P<num>\d+)/$', 'display'),
)

get_absolute_url in the xitem model is ...

def get_absolute_url(self):
return 'xitem/%s' % self.id

Any guidance will be appreciated. I have studied
https://docs.djangoproject.com/en/dev/topics/http/urls/ and experimented
with @models.permalink without success.

Thanks

Mike

bruno desthuilliers

unread,
Jun 15, 2012, 3:11:05 AM6/15/12
to django...@googlegroups.com
You have to end your url patterns with a slash, ie r'^search/'.

bruno desthuilliers

unread,
Jun 15, 2012, 3:11:05 AM6/15/12
to django...@googlegroups.com

Mike Dewhirst

unread,
Jun 15, 2012, 3:42:33 AM6/15/12
to django...@googlegroups.com
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 ... and added an extra url pattern ...

urlpatterns = patterns('item.views',
url(r'^search/$', 'search'),
url(r'^search/xitem/(?P<num>\d+)/$', 'display'),
url(r'^xitem/(?P<num>\d+)/$', 'display'),
)

... and used $ to enforce the trailing slash everywhere - and it is all
working consistently.

Thanks Bruno

Mike


>

bruno desthuilliers

unread,
Jun 15, 2012, 4:48:27 AM6/15/12
to django...@googlegroups.com
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

Mike Dewhirst

unread,
Jun 15, 2012, 7:51:58 AM6/15/12
to django...@googlegroups.com
Thanks Bruno.  I haven't looked closely at that yet. I'll see if i can do some reading this weekend.

Cheers

Mike

Connected by MOTOBLUR™


-----Original message-----
--
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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages