Laurent Caron
Laurent Caron

Reputation: 13

Why my Django URL doesn't grab the right way?

I've a problem with django url, when I go to:

/cars/my_town/my_office/

It's ok and view run as expected, town="my_town" and office_name="my_office".

When I go to:

/car/my_town/my_office/my_var1/

I get an error. When I print the vars in my views I get:

town :"my_town/my_office" and office_name:"my_var1"

My view looks like this:

def ListingCars(request,town,office_name,var1=None) :

My urls:

url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars'),
url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/(?P<var1>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars_var1'),

SOLVE ... it wasn't a resolverurl problem, but a myfault problem ;) i didn't see a "ç" on my tags name url... that cause the problem... thx for help i clean up my regex and sorry

Upvotes: 1

Views: 40

Answers (1)

Sayse
Sayse

Reputation: 43320

\W is not word, which will match a slash, you can most likely just omit that and use \w, although its not clear what your url's should match, if it is slugs then its most likely that you need [\w-]+

^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/$
^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/(?P<var1>[\w-]+)/$

What is actually happening is your first regex is matching your url so it never uses the second one, so most likely you could also fix this by switching the order of these urls so the second is first but I wouldn't recommend this.

Upvotes: 1

Related Questions