LoginSignup
0
1

More than 3 years have passed since last update.

【初心者】【Python/Django】駆け出しWebエンジニアがDjangoチュートリアルをやってみた~その3~

Last updated at Posted at 2020-10-09

はじめに

みなさん、初めまして。
Djangoを用いた投票 (poll) アプリケーションの作成過程を備忘録として公開していこうと思います。
Qiita初心者ですので読みにくい部分もあると思いますがご了承ください。

シリーズ

作業開始

オーバービュー

MVCモデルのViewとはユーザーに表示する画面を返すプログラムのことです。

投票アプリケーションでは、以下4つのビューを作成します:

質問 "インデックス" ページ -- 最新の質問をいくつか表示
質問 "詳細" ページ -- 結果を表示せず、質問テキストと投票フォームを表示
質問 "結果" ページ -- 特定の質問の結果を表示
投票ページ -- 特定の質問の選択を投票として受付

ちなみに、URLからビューを得るDjango機能をURLconfといいます

もっとビューを書いてみる

以下の動作をURLconfを用いて実装します。

ユーザが「/polls/100/」にアクセス
 →config/urls.pyのpath.'polls/'に一致するためpolls/urls.pyを読み込む
  →polls/urls.pyのpath.'100/'に一致するためviews.pyのdetail(request=, question_id=100)を返す

polls/views.py
# Create your views here.
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello,world.You're at the polls index.")


def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)


def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)


def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

question_id=100は<>で挟んでURLの一部からキャプチャします。

polls/urls.py
from django.urls import path
from . import views

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'),
]

実際に動作するビューを書く

いままで動かないもの作っていたのか・・・
違うみたいです。「実際に動作する」とは「HttpRequest処理と例外処理を実装して、実運用に沿った挙動を実現する」ということみたいです。

viewに以下を追加します。

polls/views.py
from .models import Question
from django.template import loader


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))

「template.render(context, request)」でlatest_question_listをpolls/index.htmlに渡し、レンダリングします。
Djangoのloader.get_templateの仕様で、レンダリングテンプレートを「.templates/アプリ名(polls)」ディレクトリ配下から検索するようです。

polls/templates/polls/index.html
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

ショートカットすることもできます。
django.shortcutからrenderをインポートします。

polls/views.py
from .models import Question
from django.shortcuts import render


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)

このような画面が表示されるはずです。
image.png
image.png

404 エラーの送出

まずはビューを作成します。

polls/views.py
from django.http import HttpResponse, Http404
from .models import Question
from django.shortcuts import render

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question dose not exist")
    return render(request, 'polls/detail.html', {'question': question})

続いてHTML

polls/templates/polls/detail.html
{{question}}

動作確認します。

question_id=5は存在するため、「http://127.0.0.1:8000/polls/5/」は以下のように表示されます。
image.png

question_id=999は存在しないため、「http://127.0.0.1:8000/polls/999/」は以下のように表示されます。
Exceptionをキャッチして「Question dose not exist」が表示されてます。
image.png

テンプレートシステムを使う

detailページのhtmlをマークアップしていきます。

polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

再度、「http://127.0.0.1:8000/polls/5/」を表示します。
image.png

テンプレート内のハードコードされたURLを削除

URL部分を変数化します。URLの変更時にテンプレートの修正が必要になるためです。

仕組みとしては、polls/urls.py > urlpatterns > pathで name='detail' を指定しましたね。
これをpolls/templates/polls/index.htmlで読み込みます。するとname='detail'に紐づくURLが読み込まれます。

polls/templates/polls/index.html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
polls/urls.py
urlpatterns = [
    ***
    path('<int:question_id>/', views.detail, name='detail'),
    ***
]

表示も問題ないです。

image.png

URL 名の名前空間

今回作成したアプリはpollsアプリ1つだけです。
複数アプリを作成した場合、複数アプリがdetailビューを持つ可能性があります。
pollsアプリのdetailビューを取り出すためには、名前空間を作成・指定します。

名前空間の作成は、<アプリ名>/templates/<アプリ名>/index.htmlにapp_name=<アプリ名>を追記します。

polls/urls.py
app_name = 'polls'
urlpatterns = [
    ***
    path('<int:question_id>/', views.detail, name='detail'),
    ***
]

名前空間を指定したURL指定は、<アプリ名>:detailとします。

polls/templates/polls/index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

本日はここまでにします。ありがとうございました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1