在基于类的视图中抛出NoReverseMatch

时间:2017-11-03 11:37:02

标签: django

所以我有一个列出一堆链接的视图:

class CheckpointSelectView(mixins.LoginRequiredMixin, generic.ListView):
    model               = Checkpoint
    template_name       = 'warden/checkpoint_select_view.html'
    context_object_name = 'checkpoints'

模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% block content %}
    <ul>
        {% for checkpoint in checkpoints %}
            <li><a href="{% url 'checkpoint' checkpoint_name=checkpoint.name %}">{{ checkpoint.name }}</a></li>
        {% endfor %}
    </ul>
{% endblock %}
</body>
</html>

还有一些网址:

urlpatterns = [
    url(r'^$', CheckpointSelectView.as_view(), name='checkpoint_selection'),
    url(r'^(?P<checkpoint_name>\w+-{0,1}\w+)/$', CheckpointWatchView.as_view(), name='checkpoint'),
]

当我导航到该视图时,Django会抛出一个NoReverseMatch异常,但是如果我像这样更改模板:

{% block content %}
    <ul>
        {% for checkpoint in checkpoints %}
            <li><a href="{{ checkpoint.name }}">{{ checkpoint.name }}</a></li>
        {% endfor %}
    </ul>
{% endblock %}

它有效。

我使用url标签错了吗?非常感谢任何帮助。

回溯:

NoReverseMatch at /en/warden/

Reverse for 'checkpoint' with arguments '()' and keyword arguments '{'checkpoint_name': 'start'}' not found. 1 pattern(s) tried: ['(ro|en)/warden/(?P<checkpoint_name>\\w+-{0,1}\\w+)/$']

Request Method:     GET
Request URL:    http://127.0.0.1:8000/en/warden/
Django Version:     1.10.7
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'checkpoint' with arguments '()' and keyword arguments '{'checkpoint_name': 'start'}' not found. 1 pattern(s) tried: ['(ro|en)/warden/(?P<checkpoint_name>\\w+-{0,1}\\w+)/$']

Exception Location:     /home/jotun/workspace/.virtualenvs/AESH/lib/python3.6/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 392
Python Executable:  /home/jotun/workspace/.virtualenvs/AESH/bin/python
Python Version:     3.6.2

0 个答案:

没有答案
相关问题