正如标题所示,我遇到了 NoReverseMatch 错误,但我的 url.py 具有相应的命名 url。我非常有信心我错过了一些简单的事情,但作为新人,我似乎找不到自己的问题。
<小时/>错误:(在下面的employee_edit.html 文件中生成)
Caught NoReverseMatch while rendering: Reverse for ''employee_new'' with arguments '()' and keyword arguments '{}' not found.
\home\用户名\mysite\myapp\views.py:
class v_EmployeeCreate(CreateView):
model = Employee
template_name = 'employee/employee_edit.html'
def get_success_url(self):
return reverse('employee_list')
\home\用户名\mysite\url.py:
from myapp.views import v_EmployeeCreate, v_EmployeeList
urlpatterns = patterns('',
< ... snip ...>
url(r'^newEmployee$', v_EmployeeCreate.as_view(), name="employee_new"),
)
\home\username\mysite\myapp\templates\employee\employee_edit.html(第 7 行):
<form action="{% url 'employee_new' %}" method="POST">
我觉得存在文件路径问题,但我不确定如何解决该问题。指定的 URL 可以让我访问模板,但模板本身无法生成 URL。
为了记录到目前为止,我有:
- 重新加载 django
- 检查拼写
- 已确认非模板功能(无需模板标记的相同设置即可。页面加载)
使用教程:http://effectivedjango.com/tutorial/views.html#creating-contacts
最佳答案
我认为您使用的是旧版本的 Django - 1.5 之前的版本(当前版本是 1.6)。线索是您的错误消息在 View 名称周围有两个单引号:在那些旧版本中,您不应在 url
标记中的名称周围加上引号。
您应该(最好)升级 Django,或者(如果您确实做不到)使用 {% url employee_new %}
关于python - 渲染时捕获 NoReverseMatch,但具有匹配的 URL 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481130/