I have 2 links in my html templates. First link pass only 1 parameter to URL and second link pass 2 parameters. Like this:
我的html模板中有2个链接。第一个链接只将1个参数传递给URL,第二个链接传递2个参数。喜欢这个:
<a href="/products/{{categ_name}}">{{categ_name}}</a>
<a href="/products/{{categ_name}}/{{subcateg_name}}">{{subcateg_name}}</a>
Now when i click on link with 1 parameter it works fine. I get the parameter value in my django view.
But When i click on link with two parameters i only get the first parameter. I get None in the value of second parameter.
现在,当我点击带有1个参数的链接时,它工作正常。我在django视图中获取参数值。但是当我点击带有两个参数的链接时,我只得到第一个参数。我在第二个参数的值中得到None。
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 second parametre?
如何获得第二参数的值?
1 个解决方案
#1
11
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 your 2-parameter url being matched by the first 1-parameter pattern. The $ special character means end of string.
然后,您可以避免您的2参数URL与第一个1参数模式匹配。 $ special字符表示字符串结尾。
#1
11
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 your 2-parameter url being matched by the first 1-parameter pattern. The $ special character means end of string.
然后,您可以避免您的2参数URL与第一个1参数模式匹配。 $ special字符表示字符串结尾。