如何在django中使用mako模板系统
2009-03-26 23:39
1、安装mako:
下载地址:http://www.makotemplates.org/download.html
2、安装Django-mako
下载地址:http://pypi.python.org/pypi/django-mako
参考网址:http://code.google.com/p/django-mako/wiki/Usage
Installinghttp://pypi.python.org/pypi/django-mako.
Django-mako is a normal Python package. If you have setuptools installed you can use easy_install.
$ easy_install django-mako
You can also download the tarball from PyPI,
Then extract it and run setup.py install.
$ tar -zxf django-mako-0.1.0.tar.gz
$ sudo python setup.py install
Once installed, the main module name is djangomako.
EnablingShortcutsUsing djangomako in your views
You enable django-mako like any other Django middleware, by adding a single entry to your MIDDLEWARE_CLASSES tuple in your settings.py.
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'djangomako.middleware.MakoMiddleware',
)
django-mako will also look for a MAKO_TEMPLATE_DIRS setting, if it's not set than mako will use the same TEMPLATE_DIRSsetting which is also used by the django templating system. This gives you the option of separating your mako templates from your django templates or simply keeping all your templates together.
django-mako provides shortcut functions that mimic django templates render_to_string and render_to_response.
Note: These functions to not support context instances... mostly because I don't really understand what they are suppose to be used for.
from djangomako.shortcuts import render_to_response, render_to_string
def index_view(request):
return render_to_response('index.html', {'user':request.user})
def print_index_view(request):
print render_to_string('index.html', {'user':request.user})
3、中文模板处理
参考网址:http://limodou.javaeye.com/blog/90860
在模板顶部加上编码
#coding=utf-8 或者 ## -*- coding: utf-8 -*-
2009-03-26 23:39
1、安装mako:
下载地址:http://www.makotemplates.org/download.html
2、安装Django-mako
下载地址:http://pypi.python.org/pypi/django-mako
参考网址:http://code.google.com/p/django-mako/wiki/Usage
Installinghttp://pypi.python.org/pypi/django-mako.
Django-mako is a normal Python package. If you have setuptools installed you can use easy_install.
$ easy_install django-mako
You can also download the tarball from PyPI,
Then extract it and run setup.py install.
$ tar -zxf django-mako-0.1.0.tar.gz
$ sudo python setup.py install
Once installed, the main module name is djangomako.
EnablingShortcutsUsing djangomako in your views
You enable django-mako like any other Django middleware, by adding a single entry to your MIDDLEWARE_CLASSES tuple in your settings.py.
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'djangomako.middleware.MakoMiddleware',
)
django-mako will also look for a MAKO_TEMPLATE_DIRS setting, if it's not set than mako will use the same TEMPLATE_DIRSsetting which is also used by the django templating system. This gives you the option of separating your mako templates from your django templates or simply keeping all your templates together.
django-mako provides shortcut functions that mimic django templates render_to_string and render_to_response.
Note: These functions to not support context instances... mostly because I don't really understand what they are suppose to be used for.
from djangomako.shortcuts import render_to_response, render_to_string
def index_view(request):
return render_to_response('index.html', {'user':request.user})
def print_index_view(request):
print render_to_string('index.html', {'user':request.user})
3、中文模板处理
参考网址:http://limodou.javaeye.com/blog/90860
在模板顶部加上编码
#coding=utf-8 或者 ## -*- coding: utf-8 -*-