【发布时间】:2013-09-27 22:40:53
【问题描述】:
我在视图中有两个方法 create 和 update,其中 update 接受一个参数,而 create 不接受任何参数。我决定把它们变成只有一个函数 update_create 因为它们并没有那么不同。
这是视图中的新方法的外观:
def update_create(request, id=None):
这是我的 urls.py:
url(r'^(\d+)/update/$|create/$', update_create, name='update_create'),
这就是我的模板在 templates/list.html 中的样子
<a href="{% url 'update_create' %}"> Create a new event </a>
我在使用上面的代码时遇到了这个错误:
NoReverseMatch at /agenda/list/
Reverse for 'update_create' with arguments '()' and keyword arguments '{}' not found.
但是,如果我在模板中使用它(我添加了一个参数),它可以正常工作:
<a href="{% url 'update_create' 1 %}"> Create a new event </a>
有人可以解释发生了什么吗?为什么以前的代码不起作用,为什么新代码起作用?
【问题讨论】:
-
顺便说一句,你读过这个帖子吗? stackoverflow.com/questions/4981026/noreversematch-error
-
是的,我看到了一些类似的主题,但我的更多是关于论点。谢谢:)
标签: django django-templates django-views