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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Q&A

解決済

1回答

2055閲覧

Django チュートリアルで起きたTemplateDoesNotExist at /polls/1/

waruhime

総合スコア12

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

0グッド

0クリップ

投稿2021/06/07 21:00

Python Version: 3.8.5

Djangoのチュートリアルを実行しているのですが、
チュートリアル4の/polls/1/にアクセスすると、
投票画面ではなくエラーが出てしまいます。

mysite/polls/templates/polls/detail.html (Source does not exist)
とでているので、detail.htmlのどこかが間違ってるのかと見てるのですが、原因がわからないです。

どなたか、ご教授いただけないでしょうか?
よろしくお願いいたします。

行いたいこと
"ブラウザで /polls/1/ を表示して投票してみましょう。票を入れるたびに、結果のページが更新されていることがわかるはずです。選択肢を選ばずにフォームを送信すると、エラーメッセージを表示されるはずです。"

polls/templates/polls/details.html

<h1>{{ question.question_text }}</h1> <ul> {% for choice.choice in question.choice_set.all %} <li>{{ choice.choice_text }}</li> {% endfor %} </ul> <form action="{% url 'polls:vote' question.id %}" method="post"> {% csrf_token %} <fieldset> <legend><h1>{{ question.question_text }}</h1></legend> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} {% for choice in question.choice_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 %} </fieldset> <input type="submit" value="Vote"> </form>

polls/views.py

from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.template import loader from django.urls import reverse from .models import Question #from django.shortcuts import Question from django.shortcuts import get_object_or_404, render from django.http import Http404 from .models import Choice, Question # Create your views here. def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) #def index(request): # return HttpResponse("Hello, world. You're at the polls index.") def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html',{'question': question}) def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question}) # response = "You're looking at the result of question %s." # return HttpResponse(response % question_id) def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

polls/urls.py

from django.urls import path from . import views app_name = 'polls' urlpatterns = [ path('', views.index, name='index'), path('<int:question_id>/', views.detail,name='detail'), path('<int:question_id>/results/', views.results,name='results'), path('<int:question_id>/vote/', views.vote,name='vote'), ]

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

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

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

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

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

ArMigELo

2021/06/08 00:37

なにかサイトか、教材をみてやっていますか?
guest

回答1

0

ベストアンサー

mysite/polls/templates/polls/detail.html (Source does not exist)

とあるのに対して、質問文上だと

polls/templates/polls/details.html

としてコードを掲載しています。これだけ見ると、detail.htmlであるべきところをdetails.htmlで保存しているように見えます。

投稿2021/06/08 01:22

attakei

総合スコア2738

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

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

waruhime

2021/06/08 01:26

ありがとうございます! 解決致しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問