2016-11-12 13 views
0

私は簡単なことを試していますユーザーが表示された質問をクリックします。リンクがクリックされると、質問IDが引数として渡され、関連する選択肢が表示されます。ユーザは選択肢を選択するように求められ、次のページにはその選択肢に投票した人の数(ユーザの現在の決定を含む)が表示される。 エラーは、私が127.0.0.1:8000/polls/を使ってアプリケーションを起動したときに表示される質問です。それから私が質問をクリックすると、URLは127.0.0.1:8000/polls/3/になります。これは3が質問IDであるため正しいです。したがって、質問IDの選択肢が表示されることが期待されます。しかし、それは表示されていません。 エラーは次のとおりです。DjangoテンプレートからURL経由で複数の引数を渡すにはどうしたらいいですか?

NoReverseMatch at /polls/3/ 

Reverse for 'results' with arguments '()' and keyword arguments '{'q_id': 3, 'c_test': 'May Be'}' not found. 1 pattern(s) tried: ['polls/(?P<q_id>[0-9]+)/(?P<c_test>\\w+)/results/$'] 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/polls/3/ 
Django Version:  1.10.3 
Exception Type:  NoReverseMatch 
Exception Value:  

Reverse for 'results' with arguments '()' and keyword arguments '{'q_id': 3, 'c_test': 'May Be'}' not found. 1 pattern(s) tried: ['polls/(?P<q_id>[0-9]+)/(?P<c_test>\\w+)/results/$'] 

Exception Location:  /home/usr/.local/lib/python3.5/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 392 
Python Executable: /usr/bin/python3.5 
Python Version:  3.5.2 

私のコードは次のとおりです。

views.py

class IndexView(generic.ListView): 
    template_name = "polls/index.html" 
    context_object_name = "latest_question_list" 
    model = Question 

def get_queryset(self): 
    return Question.objects.filter(
     pub_date__lte=timezone.now() 
    ).order_by('-pub_date')[:5] 


class DetailView(ListView): 
    model = Choice 
    context_object_name = "latest_choice_list" 
    template_name = "polls/detail.html" 

def get_queryset(self): 
    print(self.args[0]) 
    '''Return list of choices''' 
    return Choice.objects.filter(question_id=self.args[0]) 
    # return Choice.objects.all() 

def pollvote(request, q_id, c_test): 

if c_test: 
    p = Choice.objects.filter(question_id=q_id).get(choice_test=c_test) 
    count = p.votes 
    count += 1 
    p.votes = count 
    p.save() 

return HttpResponseRedirect('/polls/%s/%s/results/' % c_test, q_id) 

detail.html

{% load staticfiles %} 
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> 
{% if latest_choice_list %} 
    <p>Cast your Choice</p> 
<ul> 


    {% for choice in latest_choice_list %} 
      <li><a href="{% url 'polls:results' q_id=choice.question_id c_test=choice.choice_test%}">{{ choice.choice_test }}</a></li> 


    {% endfor %} 
</ul> 


{% else %} 
<p>No choice are available.</p> 
{% endif %} 

結果(エラーがHREF行で問題を言います) .html:

{% load staticfiles %} 
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> 
{% if latest_choice_list %} 
    <p>Choices Made So Far</p> 
<ul> 
    {% for choice in latest_choice_list %} 
    <li>{{ choice.choice_test }} - {{ choice.votes }} </li> 
    {% endfor %} 
</ul> 

{% else %} 
<p>No choice are available.</p> 
{% endif %} 

urls.py

urlpatterns = [ 
url(r'^$', views.IndexView.as_view(), name='index'), 
url(r'^([0-9]+)/$', views.DetailView.as_view(), name='detail'),  
url(r'^(?P<q_id>[0-9]+)/(?P<c_test>\w+)/results/$', views.pollvote, name='results'),] 

はなぜdetail.htmlはエラーを投げているのですか?なぜそれは2つのキーワードを引数とし、それを結果に渡すのではないのですか?

+0

おそらくCtrl + Vの問題ですが、views.pyの字下げは明らかに間違っています – svfat

+0

これは実際のコードでは正しくありません。トピックを投稿している間はここにコピー・ペーストの問題があります。 – debayan89

答えて

0

これを試してみてください:

<a href="{% url 'polls:results' choice.question_id choice.choice_test%}">{{ choice.choice_test }}</a> 

Djangoが自動的に与えられた順序で引数を割り当てます。

P.S.

  • 私は選択肢モデルがquestion_idとchoice_testフィールドを持っていることを前提としています。
  • question_idがQuestionオブジェクトへの参照として整数フィールドとして使用されている場合、これを行う代わりに、django.medelsのForeign keyフィールドを使用できます。 https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_one/
+0

こんにちは、あなたが言ったことをしました。でも同じです。 引数 '(3、' May Be ')'およびキーワード引数 '{}'が見つからない場合は 'results'のために反対になります。 1つのパターンが試されました:キーワード 'args'の代わりに '' polls /(?P [0-9] +)/(?P \\ w)/ results/$ '] そして、まだsysエラーが発生しました。 そして私の選択テーブルには両方のフィールドがあります。CREATE TABLE "polls_choice"( "id"整数NOT NULLプライマリキーAUTOINCREMENT、 "choice_test" varchar(200)NOT NULL、 "votes"整数NOT NULL、 "question_id"整数NOT NULL REFERENCES "polls_question"( "id")) ; – debayan89

+0

私は深くあなたのコメントを見ていないが、私は\ wだけが文字とアンダースコアにマッチすると思う。スペースと一致しません。だからurls.pyの正規表現を変更する –

関連する問題