How to pass multiple URL parameters in django

I have 2 links in my html templates. The first link passes only 1 parameter for the URL parameters and the second channel 2. Like this:

<a href="/products/{{categ_name}}">{{categ_name}}</a> <a href="/products/{{categ_name}}/{{subcateg_name}}">{{subcateg_name}}</a> 

Now when I click on the link with parameter 1, it works fine. I get the parameter value in django view.
But when I click on the link to two parameters, I get only the first parameter. I get None in the value of the second parameter.

My urls.py:

 urlpatterns = patterns('', url(r'^products/(?P<categ_name>\w+)/', views.products, name='products_category'), url(r'^products/(?P<categ_name>\w+)/(?P<subcateg_name>\w+)/', views.products, name='products_subcategory'), url(r'^logout/',views.logoutView, name='logout'),) 

My views.py:

 def products(request, categ_name=None, subcateg_name=None): print categ_name, subcateg_name ... 

How to get the value of the second parameter?

+6
source share
1 answer

Change your urls to:

 urlpatterns = patterns('', url(r'^products/(?P<categ_name>\w+)/$', views.products, name='products_category'), url(r'^products/(?P<categ_name>\w+)/(?P<subcateg_name>\w+)/$', views.products, name='products_subcategory'), url(r'^logout/',views.logoutView, name='logout'),) 

Then you avoid having your URL with two parameters match the first pattern with 1 parameter. The special character $ means the end of the line.

+10
source

All Articles