2017-08-24 1 views
0

すべてが/ APP1/2/

で汎用ビューなし

NoReverseMatchをうまく働きました

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

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 

問題は、コードのこの部分でのレンダリングテンプレート中

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 

エラーの

ビュー

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

のURL

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> 
+0

あなたはあまりにも多くの情報を与えています。 [MCVE](https://stackoverflow.com/help/mcve)を入力してください。 –

+1

'question.id'はNoneです。 – Sayse

+0

[NoReverseMatchエラーとは何ですか?また修正するにはどうすればいいですか?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i- fix-it) – Sayse

答えて

1

モデルが、MyQuestionであるため、詳細ビューはmyquestionとしてコンテキスト・インスタンスにオブジェクトを追加します。あなたのいずれかがmyquestionを使用するようにテンプレートを変更する必要があります。

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

またはビューにcontext_object_nameを設定します。

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

それです!今私はそれのための答えを探して何かをしてから数時間後に進むことができます。ありがとうございました – disperX

関連する問題