質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1693閲覧

Django エラー内容が分からない

退会済みユーザー

退会済みユーザー

総合スコア0

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2020/01/12 06:36

前提・実現したいこと

Djangoにおいて発生したエラーを解消したい
該当する箇所がどこなのか分からず困ってます
どなたかご教授頂けると幸いです
http://127.0.0.1:8000/polls/でエラーが発生

発生している問題・エラーメッセージ

error

1NoReverseMatch at /polls/ 2Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<pk>[0-9]+)/$'] 3Request Method: GET 4Request URL: http://127.0.0.1:8000/polls/ 5Django Version: 3.0.1 6Exception Type: NoReverseMatch 7Exception Value: 8Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<pk>[0-9]+)/$'] 9Exception Location: C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 677 10Python Executable: C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\python.exe 11Python Version: 3.8.0 12Python Path: 13['C:\Users\sa96t\Desktop\Django\mysite', 14 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\python38.zip', 15 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\DLLs', 16 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib', 17 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32', 18 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages'] 19Server time: Sun, 12 Jan 2020 01:19:50 +0000 20Error during template rendering 21In template C:\Users\sa96t\Desktop\Django\mysite\polls\templates\polls\index.html, error at line 8 22 23Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<pk>[0-9]+)/$'] 241 {% load static %} 252 263 <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"> 274 285 {% if latest_question_list %} 296 <ul> 307 {% for question in latest_question_list %} 318 <li><a href="{% url 'polls:detail' question_id %}">{{ question.question_text }}</a></li> 329 {% endfor %} 3310 </ul> 3411 {% else %} 3512 <p>No polls are available</p> 3613 {% endif %}

該当のソースコード

polls/templates/polls/index.html

1{% load static %} 2 3<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"> 4 5{% if latest_question_list %} 6 <ul> 7 {% for question in latest_question_list %} 8 <li><a href="{% url 'polls:detail' question_id %}">{{ question.question_text }}</a></li> 9 {% endfor %} 10 </ul> 11{% else %} 12 <p>No polls are available</p> 13{% endif %}

polls/urls.py

1from django.urls import path 2 3from . import views 4 5app_name = 'polls' 6 7urlpatterns = [ 8 #path('', views.index, name='index'), 9 #path('<int:question_id>/', views.detail, name='detail'), 10 #path('<int:question_id>/result', views.results, name='result'), 11 path('<int:question_id>/vote/', views.vote, name='vote'), 12 13 path('', views.IndexView.as_view(), name='index'), 14 path('<int:pk>/', views.DetailView.as_view(), name='detail'), 15 path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), 16]

polls/views.py

1from django.shortcuts import render 2 3from django.http import HttpResponse 4from .models import Question 5from django.template import loader 6from django.http import Http404 7 8from django.http import HttpResponse, HttpResponseRedirect 9from django.shortcuts import get_object_or_404, render 10from django.urls import reverse 11from .models import Choice, Question 12 13from django.views import generic 14from django.utils import timezone 15 16# Create your views here. 17''' 18def index(request): 19 latest_question_list = Question.object.order_bey('-pub_date')[:5] 20 #output = ', '.join([q.question_text for q in latest_question_list]) 21 #return HttpResponse(output) 22 23 #template = loader.get_template('polls/index.html') 24 context = { 25 'latest_question_list': latest_question_list, 26 } 27 #return HttpResponse(template.render(context, request)) 28 29 return render(request, 'polls/index.html', context) 30 31def detail(request, question_id): 32 #return HttpResponse("You're looking at question %s." % question_id) 33 34 #try: 35 # question = Question.objects.get(pk=question_id) 36 #except Question.DoesNotExist: 37 # raise Http404("Question doesn't exist") 38 39 question = get_object_or_404(Question, pk=question_id) 40 41 #return render(request, 'polls/detail.html', {'question': question}) 42 43 try: 44 selected_choice = question.choice_set.get(pk=request.POST['choice']) 45 except(KeyError, Choice.DoesNotExist): 46 return render(request, 'polls/detail.html', { 47 'question': question, 48 'error_message': "You didn't select a choice", 49 }) 50 else: 51 selected_choice.votes += 1 52 selected_choice.save() 53 return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 54 55def results(request, question_id): 56 #response = "You're looking at the results of questoin %s." 57 #return HttpResponse(response % question_id) 58 59 question = get_object_or_404(Question, pk=question_id) 60 return render(request, 'polls/results.html', {'question': question}) 61''' 62 63def vote(request, question_id): 64 return HttpResponse("You're voting on question %s." % question_id) 65 66class IndexView(generic.ListView): 67 template_name = 'polls/index.html' 68 context_object_name = 'latest_question_list' 69 70 def get_queryset(self): 71 #return Question.objects.order_by('-pub_date')[:5] 72 73 return Question.objects.filter( 74 pub_date__lte = timezone.now() 75 ).order_by('-pub_date')[:5] 76 77class DetailView(generic.DetailView): 78 model = Question 79 template_name = 'polls/detail.html' 80 81 def get_queryset(self): 82 return Question.objects.filter(pub_date__lte=timezone.now()) 83 84class ResultsView(generic.DetailView): 85 model = Question 86 template_name = 'polls/results.html' 87

試したこと

エラー内容と以下のサイトを参照して、
polls/templates/polls/index.htmlもしくはpolls/urls.pyのタイポだと思った
参照サイト

補足情報

以下のサイトを初めから実践しており、その6まで
参考情報

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

<li><a href="{% url 'polls:detail' pk=question_id %}">{{ question.question_text }}</a></li>

投稿2020/01/12 09:07

harinezumi.py

総合スコア282

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/12 09:13

回答の程、ありがとうございます。 該当箇所を書き換えてみましたが、同じようなエラーが発生してしまいます。
harinezumi.py

2020/01/12 09:17

こちらはどうでしょうか <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
退会済みユーザー

退会済みユーザー

2020/01/12 09:44

回答の程、ありがとうございます。 上手くいきました。 本当にありがとうございました。
harinezumi.py

2020/01/12 09:49

良かったです、回答の評価いただけたら幸いです。
harinezumi.py

2020/01/12 10:05

もし大丈夫であれば、解決済をしてください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問