[英]getting NoReverseMatch for a correct url and correct arguments
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. 我已经仔细检查了我的代码,但在此时我仍然遇到NoReverseMatch错误,无法获得正确的URL和正确的参数。
Here is my URLConfig 这是我的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: 模板中的URL称为:
<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
You have profile="super_agent"
, but in your regex [A-Za-z0-9_]+
doesn't include underscores. 您拥有
profile="super_agent"
,但是在您的正则表达式[A-Za-z0-9_]+
中不包含下划线。
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 AZ, lowercase az, digits 0-9, hyphens and underscores. 您还可以使用
[\\w-]+
,它匹配大写AZ,小写az,数字0-9,连字符和下划线。 If your primary key is an integer, then [0-9]+
should be enough. 如果您的主键是整数,则
[0-9]+
应该足够。 Putting that together, you have: 放在一起,您将:
url(r'assign/(?P<pk>[0-9]+)/profile/(?P<profile>[\w-]+)/$', views.assign_profile, name='assign_profile'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.