現在Djangoのチュートリアルをやっている初心者です。
手順通り進めていったのですが、途中でブラウザに表示をしてみたところ投稿画面に行きません。
自分なりに調べたところ、index.htmlでのpythonコードの部分が読み込まれていないのではないかという疑問があり、自分ありに調べたのですが、どうしてもわかりません。
どなたかご教授お願いします。
djangoチュートリアル:https://docs.djangoproject.com/ja/2.2/intro/tutorial01/
発生している問題・エラーメッセージ
pollsを表示させた時にブラウザ以上で「No polls are available.」だけしか表示されず、投票画面が表示されません。
該当のソースコード
html
1<!--index.html --> 2 3<!DOCTYPE html> 4<!--suppress HtmlUnknownTarget --> 5<html lang="en"> 6<head> 7 <meta charset="UTF-8"> 8 <title>index</title> 9</head> 10<body> 11{% load static %} 12<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"> 13 14{% if latest_question in latest_question_list %} 15 <ul> 16 {% for question in latest_question_list %} 17 <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> 18 19 {% endfor %} 20 </ul> 21{% else %} 22 <p>No polls are available.</p> 23{% endif %} 24</body> 25</html>
python
1#views.py 2 3# Create your views here. 4from django.http import HttpResponseRedirect 5from django.shortcuts import get_object_or_404, render 6 7from django.urls import reverse 8from django.views import generic 9from django.utils import timezone 10 11from .models import Choice, Question 12 13class IndexView(generic.ListView): 14 template_name = 'polls/index.html' 15 context_object_name = 'latest_question_list' 16 17 def get_queryset(self): 18 """Return the last five published questions.""" 19 return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5] 20 21class DetailView(generic.DetailView): 22 model = Question 23 template_name = 'polls/detail.html' 24 25 def get_queryset(self): 26 """ 27 Excludes any questions that aren't published yet. 28 """ 29 return Question.objects.filter(pub_date__lte=timezone.now()) 30 31class ResultsView(generic.DetailView): 32 model = Question 33 template_name = 'polls/results.html' 34 35def vote(request, question_id): 36 question = get_object_or_404(Question, pk=question_id) 37 try: 38 selected_choice = question.choice_set.get(pk=request.POST['choice']) 39 except (KeyError, Choice.DoesNotExist): 40 # Redisplay the question voting form. 41 return render(request, 'polls/detail.html', { 42 'question': question, 43 'error_message': "You didn't select a choice.", 44 }) 45 else: 46 selected_choice.votes += 1 47 selected_choice.save() 48 # Always return an HttpResponseRedirect after successfully dealing 49 # with POST data. This prevents data from being posted twice if a 50 # user hits the Back button. 51 return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 52 53 # return HttpResponse("You're lookig at the results of question %s." % question_id) 54 55 56# Leave the rest of the views (details, results, votes) unchanged 57 58class DetailView(generic.DetailView): 59 def get_queryset(self): 60 """ 61 Excludes any qustion that aren't published yet. 62 :return: 63 """ 64 return Question.objects.filter(pub_date__lte=timezone.now()) 65
html
1<!-- detail.html --> 2 3<!DOCTYPE html> 4<!--suppress ALL --> 5<html lang="en"> 6<head> 7 <meta charset="UTF-8"> 8 <title>question</title> 9</head> 10<body> 11 12<h1>{{ question.question_text }}</h1> 13 14{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 15 16<form action="{% url 'polls:vote' question.id %}" method="post"> 17{% csrf_token %} 18{% for choice in question.choice_set.all %} 19 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"> 20 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> 21{% endfor %} 22<input type="submit" value="Vote"> 23</form> 24 25 26</body> 27</html>
css
1li a { 2 color: green; 3}
試したこと
ミスタイピングなどを探しましたがなかったです。
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/14 08:38