您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 黔东南分类信息网,免费分类信息发布

Django 基础教程 - 模板

2024/4/29 6:51:26发布16次查看
注明:python版本为3.3.1、django版本为1.5.1,操作系统为windows7,其他版本有一些不同的地方读者可以自行探讨。
在上一章你可能已经发现了这样的问题,就是在视图返回文本的时候,html代码被硬编码在了python的代码中。如%s等等。像这样写往往使得程序更加复杂,一旦修改起来又显得十分的麻烦,而且html代码程序员不见得会python代码,现在的开发一般都会使得html前台页面和python后台分离,也就是前台只负责显示页面,后台只负责处理数据和其他操作。因此,模板显得尤为重要。
那么,什么是模板呢?
模板是一个文本,用于分离文档的表现形式和内容。 模板定义了占位符以及各种用于规范文档该如何显示的各部分基本逻辑(模板标签)。 模板通常用于产生html,但是django的模板也能产生任何基于文本格式的文档。下面我们从一个简单的例子来学习下什么是模板。(这个例子源自djangobook2)
ordering notice ordering notice dear {{ person_name }},
thanks for placing an order from {{ company }}. it's scheduled to ship on {{ ship_date|date:f j, y }}.
here are the items you've ordered:
{% for item in item_list %} {{ item }} {% endfor %} {% if ordered_warranty %} your warranty information will be included in the packaging.
{% else %} you didn't order a warranty, so you're on your own when the products inevitably stop working.
{% endif %} sincerely,
{{ company }}

如上所示用{{...}}或者{%...%}来替代python代码的方式就是模板,像第一个{{person_name}}其实就是一个变量,而{%for....%}或者{% if ...%}等就是循环。先不去深究上面的代码的意思,我们下面一步一步来学习怎么使用它。
>>> from django import template >>> t = template.template('my name is {{ name }}.') >>> c = template.context({'name': 'adrian'}) >>> print(t.render(c)) my name is adrian. >>> c = template.context({'name': 'fred'}) >>> print(t.render(c)) my name is fred.
当你看到上面的代码时你可能会急不可耐的去尝试,结果在第二行却出现了错误。一般来说唯一可能出现的错误就是:'django_settings_module'error,这是因为django搜索django_settings_module环境变量时,它被设置在settings.py中,而直接启动python shell就会导致它不知道用哪个配置文件。例如,假设mysite在你的python搜索路径中,那么django_settings_module应该被设置为:’mysite.settings’。所以为了免去设置环境变量的麻烦,我们应该这样启动python shell。
python manage.py shell
这样可以免去你大费周章地去配置那些你不熟悉的环境变量。
下面我们来分析下那段代码。
>>> from django import template #从django中导入template对象 >>> t = template.template('my name is {{ name }}.') #使用template对象的template()方法 >>> c = template.context({'name': 'adrian'}) #使用template对象的context()函数给赋值,比如name的值就是adrian,context()的()里面是一个字典 >>> print(t.render(c)) #渲染模板,也就是讲context赋值后的name的值adrian替换上面template()中的{{name}}并输出 my name is adrian. >>> c = template.context({'name': 'fred'}) >>> print(t.render(c)) my name is fred.
从上面的例子可以看出,使用模板的三步。一、调用template函数;二、调用context函数;三、调用render函数。就这么简单。
下面我们再通过几个代码来说说context()函数。
#代码段1: >>> from django.template import template,context >>> t=template('hello,{{name}}') >>> for name in ('a','b','c'): ... print(t.render(context({'name':name}))) ... hello,a hello,b hello,c #代码段2: >>> from django.template import template,context >>> person={'name':'thunder','age':'108'} >>> t=template('{{person.name}} is {{person.age}} years old!') >>> c=context({'person':person})#后面的这个person是一个字典 >>> t.render(c) 'thunder is 108 years old!' #代码段3: >>> from django.template import template,context >>> t=template('item 2 is {{items.2}}')#items.2的意思是调用items列表的第3个元素,因为列表的索引是从0开始的 >>> c=context({'items':['apple','banana','orange']}) >>> t.render(c) 'item 2 is orange'
注意:上面的items.2不能是items.-1或者其他什么负数索引。
好好观察上面三段代码,是不是就举一反三了呢?另外默认情况下,如果一个变量不存在,模板系统会把它展示为空字符串,不做任何事情来表示失败。
黔东南分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录