投稿日: | 最終更新日:
Djangoのテンプレート
Djangoでテンプレートを使用します。
テンプレート
Djangoのテンプレートとは、雛形となるhtmlとデータを組み合わせてテキストを作り出す仕組みです。Djangoでは、htmlだけでなくメール、CSVなどあらゆる種類のテキストを生成できます。
どうテンプレートを記述するか?
テンプレート内では直接Pythonのコードは記述しません。独自のタグを用いて条件分岐やループを記述します。
環境
OS | CentOS 7.1.1503 |
---|---|
pyenv | 1.1.3-5-g7dae197 |
Anaconda | 3-4.3.0 |
MariaDB | 5.5.52-1.el7 |
Apache | 2.4.6 |
mod_wsgi | 4.5.14 |
Django | 1.11.3 |
テンプレート描画
テンプレートを描画するには、view.pyでTemplateResponseオブジェクトを利用します。
①以下のようなDjangoアプリ構成を作ります。プロジェクト名が「pj1」で、アプリケーション名が「item」です。プロジェクトの設定は済ませたものとします。赤字のディレクトリとファイルは、今回作成するテンプレートファイルです。
- pj1/
- pj1/
- __init__.py
- settings.py
- urls.py
- wsgi.py
- item/
- __init__.py
- admin.py
- apps.py
- urls.py
- urls.py
- views.py
- __pycache__/
- migrations/
- templates/
- item/
- message.html
- __init__.py
- models.py
- views.py
- manage.py
②itemディレクトリ配下のurls.pyを編集します。「http://localhost/item/hello」のルーティングを作成するため、以下のように記述します。
from django.conf.urls import url from . import views urlpatterns = [ url(r'^hello/', views.hello, name='hello'), ]
③プロジェクトディレクトリ「pj1」直下のurls.pyを以下のように編集します。
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), #itemアプリケーションのURL設定 url(r'^item/', include('item.urls', namespace = 'item')), ]
④アプリケーションディレクトリ「item」に「template」ディレクトリを作ります。また、そのさらに直下に「item」ディレクトリを作成します。
mkdir template cd template mkdir item
⑤そこに、htmlのテンプレートファイル「message.html」を作成します。
vi item/message.html
以下のように記述します。
⑥view.pyを編集します。
from django.shortcuts import render from django.http import HttpResponse from django.template.response import TemplateResponse def hello(request): #テンプレートに渡す辞書 context = { 'message': 'メッセージ', } return TemplateResponse(request, 'item/message.html', context=context)
⑦動作確認をします。Djangoのwebサーバーを起動して、webブラウザから以下のURLにアクセスします。
http://ローカルのipアドレス/item/hello
以下のようなページが表示されます。
TemplateResponseクラスのコンストラクタに渡した辞書のキーは、テンプレート内で「{{ <キー名> }}」と記述すると、キーに対応する値を出力できます。
変数の表示
viewで本日の日付を格納した変数「today」とメッセージを格納した変数「message」を定義します。これらをテンプレートで表示します。
①view.pyを編集します。
from django.shortcuts import render from datetime import datetime def hello(request): d = { 'today': datetime.now(), 'message': 'Sample message', } return render(request, 'item/index.html', d)
②itemディレクトリ配下のurls.pyを編集します。「http://localhost/item/」のルーティングを作成するため、以下のように記述します。
from django.conf.urls import url from . import views urlpatterns = [ url(r'^hello/', views.hello, name='hello'), ]
③「template/item」ディレクトリにhtmlのテンプレートファイル「index.html」を作成します。
vi item/index.html
以下のように記述します。
<!DOCTYPE html> <html> <head lang="ja"> <meta charset="UTF-8"> <title></title> </head> <body> {{ today.year }}年<br> {{ today.month }}月<br> {{ today.day }}日<br> {{ today.hour }}時です。<br> {{ message }} </body> </html>
④動作確認をします。Djangoのwebサーバーを起動して、webブラウザから以下のURLにアクセスします。
http://ローカルのipアドレス/item/
以下のようなページが表示されます。
- Python 114
- 制作 54
- RaspberryPi 41
- Django 40
- WordPress 40
- Linux 27
- VPS 22
- JavaScript 21
- PHP 20
- HTML・CSS 19
- AWS 16
- 仮想環境 15
- レスポンシブデザイン 13
- マイコン 11
- WEB全般 11
- 動画製作 9
- Webサービス 8
- 統合開発環境 8
- 機械学習 8
- PyCharm 7
- jQuery 7
- AfterEffects 7
- 起業・設立 7
- Django REST framework 6
- C# 6
- デザイン 6
- SEO 6
- pydata 6
- Visual Studio 5
- 数学 5
- 携帯サイト 5
- heroku 5
- Mac 5
- illustrator 5
- node.js 5
- Anaconda 5
- Nginx 4
- Jupyter Notebook 4
- インフラ 4
- Google Colaboratory 4
- symfony 4
- Webスクレイピング 3
- photoshop 3
- Go言語 3
- PC 3
- ツール 3
- Docker 3
- facebook 3
- 作業効率化 3
- データベース 3
- Cloud9 3
- コマンド 2
- micro:bit 2
- Kali Linux 2
- Webサーバー 2
- MariaDB 2
- ドローン 2
- コンテナ 2
- DaVinci Resolve 2
- ネットワーク 2
- Java 2
- movie 2
- PCDJ 2
- 音楽 2
- XSERVER 2
- Ansible 1
- Vue.js 1
- JSON 1
- Bootstrap 1
- バージョン管理システム 1
- SSL 1
- S3 1
- ムームードメイン 1
- ネットワーク 1
- アニメーション 1
- D3.js 1
- Rhino 1
- アニメ 1
- git 1
- windows 1
- アクセス解析 1
- スマートフォン 1
- アフィリエイトノウハウ 1
- 知識 1
- TypeScript 1
- 役立つ本・書籍 1
- データサイエンス 1
- ESP32 1
- AI 1
- ownCloud 1
- API 1