テンプレートファイルを作成する。
data1:{{ data1 }}
==============================
{% for val in list1 %} — {{ val }}
{% endfor %}
テンプレートファイルをおくパスをDjangoの設定に追加し、テンプレートをロードし、テンプレートに変数を渡す。
import django.conf
import django.template
import django.template.loader# TEMPLATE_DIRSにテンプレートファイルをおくパスを指定してDjangoの手動設定を行う。
if django.conf.settings.configured == False:
django.conf.settings.configure(
DEBUG=True, TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=(‘D:/data/template1′, ‘C:/data/template2′))
#(なぜかパスは1つだけだとエラーになってしまう)# テンプレートを取得する
tmpl = django.template.loader.get_template(‘test.txt’)# テンプレートに渡す変数を設定して、レンダリングを行う。
print tmpl.render(django.template.Context({‘data1′ : ‘果物’,
‘list1′: (‘りんご’,'みかん’,'バナナ’,'レモン’) }))
Last 5 posts in Python
- Pythonで文字コードを指定してファイルを読み取り、Unicodeとして日本語を扱うには - February 23rd, 2010
- C#とIronPython 2.6の連携方法(変数の受け渡し方) - February 20th, 2010
- Django1.1.1をIronPython2.6にインストールし、importするとエラー - February 20th, 2010