没有通用视图,一切工作正常


  / 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>

最佳答案

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

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


或在视图中设置context_object_name

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

关于python - 找不到带有参数(('',)'的'vote'反向。尝试了1个模式:['app1/(?P <question_id> [0-9] +)/票/$'],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45855143/

10-11 13:14