未找到参数 '('',)' 的反向“投票”。尝试了 1 个模式:['app1/(?P[0-9]+)/vote/$']

2023-12-23

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

/app1/2/ 处无反向匹配

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 %}

view

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

urls

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'),
]

Template

<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 %}

or set context_object_name视图中:

class DetailView(generic.DetailView):
    model = MyQuestion
    context_object_name = 'question'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未找到参数 '('',)' 的反向“投票”。尝试了 1 个模式:['app1/(?P[0-9]+)/vote/$'] 的相关文章

随机推荐