用了一个月后,终于忍受不了Django的模板了。主要原因是模板内不能运行原生的python语句,所以用起来总感觉被人绑住手脚,遍历个字典都要搞半天。

所以决定用第三方的模板。查了一下,django用的第三方模板中,性能较好的是mako和jinja2。看了它们的语法后,发现mako会更为简洁,所以选择了mako。

1.安装

安装mako,使用 pip install Mako 命令 安装mako

安装django-mako  下载https://pypi.python.org/pypi/django-mako  然后用命令 python setup.py install 安装

有时安装会一直停在Reading http://pypi.python.org/simple/MyghtyUtils/,这是已经安装成功,直接ctrl+c关闭进程即可

2.在项目的setting.py文件所在的目录下新建一个mymako.py

#mymako.py
from django.template.context import Context
from django.http import HttpResponse
from mako.template import Template
from mako.lookup import TemplateLookup
import os def render_to_response(t,c=None,context_instance=None):
path = os.path.join(os.path.dirname(__file__), 'template/')
mylookup = TemplateLookup(directories=[path],output_encoding='utf-8')
mako_temp = mylookup.get_template(t)
if context_instance:
context_instance.update(c)
else:
context_instance = Context(c)
data = {}
for d in context_instance:data.update(d)
return HttpResponse(mako_temp.render(**data))

path参数是模板所在的目录

3.在views中调用

def test(request):

        from mymako import  render_to_response
return render_to_response("test1.html",{"name":"kevin"})

4.mako模板中的继承
index.html

## index.html
<%inherit file="base.html"/> <%def name="body()">
some html code
</%def>

base.html

## base.html
<html>
<body>
<div class="header">
hello
</div> ${self.body()} <div class="footer"> </div>
</body>
</html>

5.python语句

<div  id="read">
% for key in result_dict: <p>
<b>${key_ench[key]} </b> : ${result_dict[key]}
</p>
% endfor
<p>
<b>服务器 </b> : ${server_role}
</p> </div>

6.包含一个HTML文件

<%include file="metric_header.html",args="realtime_server_conf=realtime_server_conf" />

参考资料:

http://duka.iteye.com/blog/634208

http://wenku.baidu.com/link?url=IFCIRrDgu0qnf29DjeSoroIiGIhSypoIuqfW2tliPVdWrycAhBCzi4ELGTxJzrLznBvHOUqkIIGOK-MiDkDXnYbSqmhKFaMtQT_U-db4pM3

http://www.sandzhang.com/blog/2010/04/03/install-mako-templates-and-plugin-for-django/

DEBUG

以前

1.在mako模板中,python块<% %>要放在 def里面,不然会弹出“undefined"错误,例如变量的定义  a='aa'

2.当模板报错 <mako.runtime.Undefined object at 0x2edd750> 时是因为模板中存在没定义的变量

3.当模板报错 Undefined  时是因为模板中存在没定义的变量

10-17

报错

(IndentationError) expected an indented block (line ) ('for key in result_dict:\rpass') in file '/data/web/mcyw/mcyw_web/mcyw_web/template/cmdb_server_report1.html' at line:  char: 

原因是使用了pycharm新建一个html文件,可能是pycharm新建的文件有问题,每一行都加了\rpass,所以模版报错,复制一个原谅的html文件,然后再上面修改就没有问题了

在Django中使用Mako模板的更多相关文章

  1. Django中如何查找模板

    参考:http://my.oschina.net/zuoan001/blog/188782 Django的setting中有关找模板的配置有如下两个: TEMPLATE_LOADERS TEMPLAT ...

  2. python2 + Django 中文传到模板页面变Unicode乱码问题

    1.确保views页面首行设置了默认编码   # -*-coding:utf-8 -*- 2.确保html页面的编码为 utf-8 3.确保项目setting文件设置了 LANGUAGE_CODE = ...

  3. Django中的Templates

    1.定义: 定义和flask框架中的是一样的,唯一的不同就是Django中有自己的模板引擎,并非Jinja2,因此有一些不同之处. 2.模板的设置 在 settings.py 中 设置 TEMPLAT ...

  4. django url路径与模板中样式相对路径的问题

    static目录下有css和js及image等文件夹,里面放置网站的一些静态文件,static位于网站根目录下,django中配置静态文件这个就细说,网上都有,昨天在添加新内容时发现一个问题,我的ur ...

  5. Django 中url补充以及模板继承

    Django中的URL补充 默认值 在url写路由关系的时候可以传递默认参数,如下: url(r'^index/', views.index,{"name":"root& ...

  6. Django中模板使用

    第一步:配置 1.在工程中创建模板目录templates. 2.在settings.py配置文件中修改TEMPLATES配置项的DIRS值:TEMPLATES = [ { 'BACKEND': 'dj ...

  7. Django(四)模板文件中的循环

    编辑views.py from django.shortcuts import render from django.shortcuts import HttpResponse #此行增加 # Cre ...

  8. django中模板变量与内置标签以及过滤器

    本文参考 官方文档 . 一  模板变量 格式: {{ variable_name }} variable_name   命名规则与变量命名规则类似,允许字符数字下划线,不允许标点. variable_ ...

  9. Django中ORM模板常用属性讲解

    学习了ORM模板中常用的字段以及使用方法,具体如下: from django.db import models # Create your models here. # 如果要将一个普通的类映射到数据 ...

随机推荐

  1. Web版的各种聊天工具

    直到近期为止,我们经常使用的即时聊天工具(QQ.msn等)了Web版,大家不用下载庞大软件,直接打开网页就能够与自己的好友聊天,非常方便.在此将时汇总        便于大家查找        节约大 ...

  2. android96 内存创建图片副本,画画板

    package com.itheima.copy; import android.os.Bundle; import android.app.Activity; import android.grap ...

  3. jedis访问redis学习笔记

    最近在学习redis,在网上查了些文章,利用他人已有的知识,总结写下了这篇文章,大部分内容还是引用别人的文章内容.经过测试发现spring-data-redis现在有的版本只能支持reids 2.6和 ...

  4. Hibernate事务管理

    User类: public class User implements Serializable{ public User(){} private Integer id; private String ...

  5. elasticsearch学习一、安装和配置

    原文链接:http://jingyan.baidu.com/article/48206aead42b53216bd6b372.html ElasticSearch是一个基于Lucene的搜索服务器.它 ...

  6. Linux理念

    1.小即是美 2.让程序只做好一件事 3.可移植性比效率更重要 4.一切即文件——使用方便而且吧硬件作为文件处理是安全的 5.使用shell脚本来提高效率和可移植性 6.避免使用可定制性低下的用户界面 ...

  7. 解读Spring Ioc容器设计图

    在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...

  8. Android 设置ListView不可滚动 及在ScrollView中不可滚动的设置

    http://m.blog.csdn.net/blog/yusewuhen/43706169 转载请注明出处: http://blog.csdn.net/androiddevelop/article/ ...

  9. Python入门 学习笔记 (一)

    原文地址:http://www.cnblogs.com/lujianwenance/p/5939786.html 说到学习一门语言第一步就是先选定使用的工具(环境).因为本人是个小白,所以征求了一下同 ...

  10. 贪心算法:旅行商问题(TSP)

    TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干个城市,任何两个城市之间 ...