Herman
Herman

Reputation: 1

Slug field followed by url

I just started Django and Python, so Im still new to this.. This is my urls.py:

url(r'(?P<slug>[-\w]+)/$','person_detail'),
url(r'(?P<slug>[-\w]+)/delete/$','person_delete'),

The problem is that when I try to do to the url: slug/delete/ it's looking for that whole part slug/delete/ as the slug. When i remove the $ in the 1st url it does not go to the person_delete view, but goes to the person_detail view, ignoring the /delete/ part Any ideas?

Upvotes: 0

Views: 4729

Answers (4)

Evgeny Zislis
Evgeny Zislis

Reputation: 6957

Note that slug fields might also include digits (not just letters and the dash), so you want to alter it to say something like:

SLUG = '(?P<slug>[\w\d-]+)'

url(r'^'+SLUG+'/delete$', delete_method, {}, 'delete_url_name')

Upvotes: 1

shiberz
shiberz

Reputation: 84

url(r'(?P<slug>[-\w]+)/delete/$','person_delete'),
url(r'(?P<slug>[-\w]+)/','person_detail'),

Url order is important in such cases, because url dispacher using first match. Common url should be last.

Upvotes: 0

Dominic Rodger
Dominic Rodger

Reputation: 99751

Try adding a leading ^:

url(r'^(?P<slug>[-\w]+)/$','person_detail'),
url(r'^(?P<slug>[-\w]+)/delete/$','person_delete'),

That said, without the leading ^ I'd expect foo/delete/ to get you to the person_detail view with slug as delete, rather than foo/delete.

Upvotes: 6

AKX
AKX

Reputation: 168824

How about something like

url(r'(?P<slug>[^/]+)/$','person_detail'),
url(r'(?P<slug>[^/]+)/delete/$','person_delete'),

to make sure the slug can not contain a slash? You could also try having the rules in the opposite order, to try have Django match /.../delete/ first.

Upvotes: 1

Related Questions