views:

270

answers:

3

Hi, 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?

+2  A: 

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.

Dominic Rodger
When - is the first character of a character class, it does not denote a range, but is instead a literal -.
AKX
@AKX - thanks. Edited with what I *think* might be the problem.
Dominic Rodger
A: 

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.

AKX
`[-\w]+` won't match a `/`, either. `\w` is a synonym for `[a-zA-Z0-9_]`, and `-` (at the beginning of end of a set) is, well, a dash.
mipadi
A: 
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.

shiberz
Not in this case, since the OP ended his URLs with the `$` sign, which matches the end of the string.
mipadi
Oops.. i misunderstood question - i think that he delete $ from first url, that caused all troubles.
shiberz