Passing ID to Django URL

I want to pass userid from django url to my view

Here is what I wrote in the Django template

 <a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" >Know More</a></div> 

To handle this URL, I wrote Url as

 url(r'^User/(\d{userid})/$', 'search.views.user_detail',name='user_details'), 

But I get an error ie

 NoReverseMatch at /search/ Reverse for ''user_details'' with arguments '(2L,)' and keyword arguments '{}' not found. 

Please help me. What can I do wrong here.

+7
source share
2 answers

No quote ''

 <a href ={% url user_details x.id %} class='btn btn-primary' style="float: right;" > Know More </a> 

Your other url

 url(r'^User/(?P<userid>\d+)/$', 'search.views.user_detail', name='user_details'), 
+12
source

Be careful, after Django 1.5 use should use quotation marks. I came across this solution and tried, got an error. I am using Django 1.6 and you need quotes:

 <a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" > Know More </a> 

hope this helps.

+7
source

All Articles