如何在django中使用mako模板系统

如何在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 -*-


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值