Pankaj Pankaj - 1 month ago 6
Python Question

string parameter passing not working in django1.9

I am passing a string parameter in view but its not working.

url(r'^users/(?P<user_type>\w+)/$', views.users, name='users'),
url(r'^users/$', views.users, name='users')


view is:-

def users(request, user_type=None):


Link is:-

<a href="{% url 'users' %}">All Users</a>
<a href="{% url 'users' customers %}">Customers</a>
<a href="{% url 'users' promoters %}">Promoters</a>


But its giving error when i access view without parameter

Reverse for 'users' with arguments '('',)' and keyword arguments '{}' not found. 2 pattern(s) tried: ['administrator/users/$', 'administrator/users/(?P<user_type>\\w+)/$']

Exception Value: Reverse for 'users' with arguments '('',)' and keyword arguments '{}' not found. 2 pattern(s) tried: ['administrator/users/$', 'administrator/users/(?P<user_type>\\w+)/$']

Answer

Use this and check

url(r'^users/(?P<user_type>\w+)/$', views.users, name='users_type'),
url(r'^users/$', views.users, name='users')

Link is:

<a href="{% url 'users' %}">All Users</a>
<a href="{% url 'users_type' "customers" %}">Customers</a>
<a href="{% url 'users_type' "promoters" %}">Promoters</a>