前提・実現したいこと
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まで
参考情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/12 09:13
2020/01/12 09:17
退会済みユーザー
2020/01/12 09:44
2020/01/12 09:49
2020/01/12 10:05