巳年じゃないけど Python やろうぜ(その 7)
もうちょっとだけ hello ページをいじるよ。
hello ページを作る(続き)
テンプレートを使い回す
Django には共通のテンプレートを用意してそれを使い回す方法が用意されています。extends
と include
です。前者はベースとなるテンプレートに個別のテンプレートを埋め込むイメージ、後者は共通する部分的なテンプレートを呼び出すイメージです。今回は extends
を紹介します。
共通のテンプレートを用意する
django_test\hello\templates\hello\common.html
を以下の内容で作成しておきます。
<!doctype html> <html> <head> <title>{{ page_title | title }}</title> </head> <body> {% block content %}{% endblock %} </body> </html>
title
はフィルターの一種で、単語の先頭の文字を大文字に変換するものです。
django_test\hello\templates\hello\form.html
{% extends './common.html' %} {% block content %} <form action="." method="post"> {% csrf_token %} {{ form.as_p }} <p><input type="submit" value="送信"></p> </form> {% endblock %}
extends
で共通のテンプレートを呼び出し、そこに独自のテンプレートを埋め込みます。ちょっと Rails と似ていますね。
django_test\hello\templates\hello\thanks.html
(django_test\hello\templates\hello\hello.html
から名前変更)
{% extends './common.html' %} {% block content %} <p>Hello, {{ your_name | title }}!</p> {% endblock %}
django_test\hello\views.py
from django.shortcuts import render from django.http import HttpResponseRedirect from .forms import NameForm def thanks(request): return render(request, 'hello/thanks.html', { 'page_title': 'hello page', 'your_name': request.session['your_name'], }) def get_name(request): if request.method == 'POST': form = NameForm(request.POST) if form.is_valid(): request.session['your_name'] = form.cleaned_data['your_name'] return HttpResponseRedirect('thanks/') else: form = NameForm() return render(request, 'hello/form.html', { 'page_title': 'name entry', 'form': form, })
render
するときに page_title
も一緒に渡すようにします。
django_test\hello\urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.get_name), url(r'^thanks/$', views.thanks), ]
ビュー関数の名前を変更したことによる変更です。
次回は静的ファイル(CSS, JavaScript など)の配置について。