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

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

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

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

Python

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

Q&A

解決済

1回答

1461閲覧

Django チュートリアル index.htmlでのpythonのコードが読み込まれない

退会済みユーザー

退会済みユーザー

総合スコア0

Django

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

Python

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

0グッド

1クリップ

投稿2019/08/13 19:00

現在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/ツールのバージョンなど)

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

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

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

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

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

guest

回答1

0

ベストアンサー

次のところを変更すると表示されるようになるのではないかと思いますが、いかがでしょう。

patch

1- {% if latest_question in latest_question_list %} 2+ {% if latest_question_list %}

パッと拝見するかぎり、この if 文のところでは latest_question という変数は未定義で、そのために、 Question モデルオブジェクトの登録の有無にかかわらず else のところに流れてしまうのではないかと思います。

投稿2019/08/14 06:36

gh640

総合スコア1407

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

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

退会済みユーザー

退会済みユーザー

2019/08/14 08:38

ご回答ありがとうございます。 再度確認したところ間違っていました。 ありがとうございます。 今後は一つ一つ確認していきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問