Reputation: 161
I have looked thoroughly my code and yet I am still stuck at this point where I get the NoReverseMatch error for correct url and correct parameters.
Here is my URLConfig
url(r'profile/$', views.agent_profile, name='agent_profile'),
url(r'profile/(?P<pk>[A-Za-z0-9]+)/$', views.change_profile, name='change_profile'),
url(r'assign/(?P<pk>[A-Za-z0-9]+)/profile/(?P<profile>[A-Za-z0-9]+)/$', views.assign_profile, name='assign_profile'),
the view handling that request is :
def assign_profile(request, pk, profile):
agent = Agent.objects.get(pk=pk)
# profile = Profile.objects.filter(designation=profile)
# profile.agents = agent
return render(request,
'portfolio/change_profile.html',
{'agent': agent})
and the url in the template is called as follow:
<li><a href={% url 'portfolio:assign_profile' pk=agent.code_agent profile="super_agent" %}>Super Agent</a></li>
and the error is as follow:
NoReverseMatch at /portfolio/profile/1/
Reverse for 'assign_profile' with keyword arguments '{'pk': '1', 'profile': 'super_agent'}' not found. 1 pattern(s) tried: ['portfolio/assign/(?P<pk>[A-Za-z0-9]+)/profile/(?P<profile>[A-Za-z0-9]+)/$']
Request Method: GET
Request URL: http://localhost:8000/portfolio/profile/1/
Django Version: 1.11
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'assign_profile' with keyword arguments '{'pk': '1', 'profile': 'super_agent'}' not found. 1 pattern(s) tried: ['portfolio/assign/(?P<pk>[A-Za-z0-9]+)/profile/(?P<profile>[A-Za-z0-9]+)/$']
Exception Location: C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable: C:\Users\admin\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\admin\\PycharmProjects\\trial',
'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
Server time: Wed, 3 Jan 2018 14:35:49 +0000
Upvotes: 0
Views: 139
Reputation: 308889
You have profile="super_agent"
, but in your regex [A-Za-z0-9_]+
doesn't include underscores.
url(r'assign/(?P<pk>[A-Za-z0-9]+)/profile/(?P<profile>[A-Za-z0-9_]+)/$', views.assign_profile, name='assign_profile'),
You could also use [\w-]+
, which matches uppercase A-Z, lowercase a-z, digits 0-9, hyphens and underscores. If your primary key is an integer, then [0-9]+
should be enough. Putting that together, you have:
url(r'assign/(?P<pk>[0-9]+)/profile/(?P<profile>[\w-]+)/$', views.assign_profile, name='assign_profile'),
Upvotes: 2