Django教程第4课的问题:/ enquete / 2 /处的NoReverseMatch

时间:2019-03-02 17:43:48

标签: python django

我遇到以下错误

  

未找到带有参数'(2,)'的'vote'反向字符。尝试了1种模式:['enquete \ / \

这是我的回溯

Internal Server Error: /enquete/2/
Traceback (most recent call last):
  File "/home/johnl/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/template/defaulttags.py", line 442, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/home/johnl/.local/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/home/johnl/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'vote' with arguments '(2,)' not found. 1 pattern(s) tried: ['enquete\\/\\<int\\:question_id\\/vote\\/$']
[02/Mar/2019 17:18:39] "GET /enquete/2/ HTTP/1.1" 500 112049
^C%  

这是我的观点,view.py

 class DetailView(generic.DetailView):
      model = Question
      template_name = 'enquete/detail.html'

这是我的网址,url.py

  app_name = 'enquete'
  urlpatterns = [
      path('',views.IndexView.as_view(), name='index'),
      path('<int:pk>/',views.DetailView.as_view(),name='detail'),
      path('<int:pk>/results',views.ResultsView.as_view(),name='results'),
      path('<int:question_id/vote/',views.vote,name='vote'),
 ]

这是我的模板detail.html

<h1> {{ question.question_text }} </h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'enquete:vote' question.id %}" method="get">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice {{forloop.counter}}" value="{{choice.id}}">
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}    
<input type="submit" value="Vote">
</form>

我需要在模型中添加投票吗?如果是,如何。我对下一步该怎么做感到震惊。

谢谢。

1 个答案:

答案 0 :(得分:3)

您的网址路径错误,请按以下说明更正。错位的>

path('<int:question_id>/vote/',views.vote,name='vote'),