python - 渲染时捕获 NoReverseMatch,但具有匹配的 URL 名称

标签 python django url error-handling

正如标题所示,我遇到了 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。

为了记录到目前为止,我有:

  1. 重新加载 django
  2. 检查拼写
  3. 已确认非模板功能(无需模板标记的相同设置即可。页面加载)

使用教程: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/

相关文章:

python - 如何将空查询集指定为非空查询集 : [ ] to [<object: hi>]

python - fatal error : Python. h:没有此类文件或目录错误:命令 'x86_64-linux-gnu-gcc' 失败,退出状态为 1

python - ModelChoiceField 的 Django 表单查询集

php - 将 Url 传递到另一个页面的最佳方式

python - 在 GTK+/Python 中设置应用程序菜单名称(修复 "Unknown Application Name")

python - 纠正 django 中不正确的序列化

python - 需要 Django 表单集

javascript - 如何在带有 JS 的模板中显示包含 HTML 和 JavaScript 的 Python 字符串列表

url - URL主机中可以显示哪些有效字符?

python - 使用 python 获取 YouTube 播放列表网址