问题与Django教程第4课:NoReverseMatch at / enquete / 2 /

问题描述 投票:0回答:1

我有以下错误

反向'投票',参数'(2,)'未找到。尝试过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>

我需要在模型中添加投票吗?如果是的话,如何。我对下一步应该做的事情非常感兴趣。

谢谢。

python django
1个回答
热门问题
推荐问题
最新问题