【问题标题】:Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']使用未找到参数“(”,)”的“投票”反向。尝试了 1 种模式:['app1/(?P<question_id>[0-9]+)/vote/$']
【发布时间】:2017-08-24 07:10:12
【问题描述】:

没有通用视图一切正常

/app1/2/ 处的 NoReverseMatch

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/app1/2/
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']
Exception Location: C:\Python36\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable:  C:\Python36\python.exe
Python Version: 3.6.1
Python Path:    
['C:\\Users\\Tomasz\\Desktop\\GitHub\\DjangoTutorialProject',
 'C:\\Python36',
 'C:\\Python36\\python36.zip',
 'C:\\Python36\\DLLs',
 'C:\\Python36\\lib',
 'C:\\Python36\\lib\\site-packages']
Server time:    Thu, 24 Aug 2017 08:34:58 +0200

模板渲染时出错

In template C:\Users\Tomasz\Desktop\GitHub\DjangoTutorialProject\app1\templates\app1\detail.html, error at line 5

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']

    1   <h1>{{ question.question_text }}</h1>
    2   
    3   {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    4   
    5   <form action="{% url 'app1:vote' question.id %}" method="post">
    6   {% csrf_token %}
    7   {% for choice in question.mychoice_set.all %}
    8       <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    9       <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
    10  {% endfor %}
    11  <input type="submit" value="Vote" />
    12  </form>
    13  

问题是这段代码:

{% url 'app1:vote' question.id %}

查看

class DetailView(generic.DetailView):
    model = MyQuestion
    template_name = 'app1/detail.html'  

网址

from django.conf.urls import url

from . import views

app_name = 'app1'
urlpatterns = [
    #url(r'^contact/$', views.contact_view, name="contact_view"),
    #url(r'^appinfo/$', views.appinfo_view, name="appinfo_view"),

    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

模板

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

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'app1:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.mychoice_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】:

您的模型是MyQuestion,因此详细视图将对象作为myquestion 添加到上下文实例中。您要么需要更改您的模板以使用myquestion

{% url 'app1:vote' myquestion.id %}

或在视图中设置context_object_name

class DetailView(generic.DetailView):
    model = MyQuestion
    context_object_name = 'question'

【讨论】:

  • 就是这样!现在我可以在几个小时后无所事事地继续寻找答案。谢谢
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 2020-10-21
  • 2019-09-07
  • 2021-12-12
  • 2020-05-13
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多