我可以访问所需的URL,但当它作为超链接添加到另一个模板中时,模板无法呈现。
因此,当我尝试访问http://127.0.0.1:8000/jobs/97072947-looking-for-woodcutter/add-milestones/
时,它会起作用。
但是当我想在 project_milestones 的模板中插入上述网址时:
{% url 'projects:create_milestone' %}
当我打开http://127.0.0.1:8000/jobs/97072947-looking-for-woodcutter/milestones/
:
Request Method: GET
Request URL: http://127.0.0.1:8000/jobs/97072947-looking-for-woodcutter/milestones/
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value: Reverse for 'create_milestone' with no arguments not found. 1 pattern(s) tried: ['jobs/(?P<project>[-\\w]+)/(add-milestones)/$']
Python Version: 3.6.1
views.py:
def project_milestones(request, project):
project = get_object_or_404(Project, slug=project)
...code..
return render(request, 'projects/employer_milestones_project.html' {'project': project,})
def create_milestone(request, project):
project = get_object_or_404(Project, slug=project)
..code...
return render(request, 'projects/employer_create_milestone.html',
{'project': project})
urls.py:
url(r'^'r'(?P<project>[-\w]+)/(add-milestones)/$',
create_milestone, name='create_milestone'),
url(r'^'r'(?P<project>[-\w]+)/(milestones)/$',
project_milestones, name='project_milestones'),
我在这里缺少什么?我也试过{% url 'projects:create_milestone' project=project.slug %}
但似乎没有任何效果。此外,如果我删除该内置url标记,页面将成功加载。
我确信答案就在documentation或here的某处,但我无法理解。对不起,如果问题结构不合理。谢谢!
答案 0 :(得分:0)
您的网址格式存在两个问题。删除字符串中的r'
,并删除add-milestone和里程碑周围的括号。
url(r'^(?P<project>[-\w]+)/add-milestones/$',
create_milestone, name='create_milestone'),
url(r'^(?P<project>[-\w]+)/milestones/$',
project_milestones, name='project_milestones'),