Python 与#x27相反;投票';带参数';(';';,)';没有找到。尝试了1种模式:[';app1/(?P<;问题id>;[0-9]+;)/投票/$&]

Python 与#x27相反;投票';带参数';(';';,)';没有找到。尝试了1种模式:[';app1/(?P<;问题id>;[0-9]+;)/投票/$&],python,django,Python,Django,没有通用视图,一切都很好 NoReverseMatch at/app1/2/ 看法 网址 从django.conf.url导入url 从…起导入视图 应用程序名称='app1' URL模式=[ #url(r'^contact/$,views.contact\u view,name=“contact\u view”), #url(r“^appinfo/$”,views.appinfo\u view,name=“appinfo\u view”), url(r'^$',views.IndexView.

没有通用视图,一切都很好

NoReverseMatch at/app1/2/

看法

网址

从django.conf.url导入url
从…起导入视图
应用程序名称='app1'
URL模式=[
#url(r'^contact/$,views.contact\u view,name=“contact\u view”),
#url(r“^appinfo/$”,views.appinfo\u view,name=“appinfo\u view”),
url(r'^$',views.IndexView.as_view(),name='index'),
url(r'^(?P[0-9]+)/$',views.DetailView.as_view(),name='detail'),
url(r'^(?P[0-9]+)/results/$',views.ResultsView.as_view(),name='results'),
url(r'^(?P[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>
{{question.question_text}
{%if error\u message%}{{{error\u message}{%endif%}
{%csrf_令牌%}
{%用于问题中的选项。mychoiceu set.all%}
{{choice.choice_text}}
{%endfor%}
您的模型是
MyQuestion
,因此详细视图将对象作为
MyQuestion
添加到上下文实例中。您需要更改模板以使用
myquestion

{% url 'app1:vote' myquestion.id %}
或在视图中设置
context\u object\u name

class DetailView(generic.DetailView):
    model = MyQuestion
    context_object_name = '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>
{% url 'app1:vote' myquestion.id %}
class DetailView(generic.DetailView):
    model = MyQuestion
    context_object_name = 'question'