Django 学习纪要(1) 模版语法

因为将来有一个 web 项目会用 python 做,决定从现在开始学 django.
之前也试过一次 django,不过看着 step by step 之类的教程学实在是慢,我个人比较喜欢的方式是先拿一个不大不小的项目代码来直接看源代码学习。今天偶然发现 http://china-django.com/ 这个网站提供源代码,地址是 http://china-django.com/files/beyond.zip,恩,不错。就看他的代码学吧。

下面开始是我的学习记录。
先从源代码开始,/template 目录下放的是模版。这个模版目录是可以在根目录下的 settings.py 中设置的,暂时不去管他。直接读模版代码吧。

/templates 根目录下几个模版是通过继承的方式逐步细化定义的。

> 模版后缀名是 htx
> 块的语法

 {% block extrahead %}{% endblock %}

> 模版可以继承,语法:

 {% extends "base.htx" %}

> 继承下来的子模版里面可以定义新的块

> load 语法是干什么用的?
 好像是从外部某个地方加载一个块

 {% load list_forum %}

> 块可以嵌套
 常见的做法是在父模版里面预先定义好一个块,在子模版里面具体写块的内容。

> 控制结构

 {% if expression %}
 {% else %}
 {% endif %}
limodou: if 后不能跟表达式,只能是简单变量

 {% if variable %}
 {% else %}
 {% endif %}

这里 if 可以是 if not.

> user 好像是一个预定义对象,类似于 asp.net 中的 User 对象。一个典型的代码如下:

 {% if user.is_anonymous %}
  您还没有登陆.
  
  
< href ="/auth/login/"  id ="SignIn" > 登陆 </ a >
  或
  
< href ="/auth/register/"  id ="Register" > 注册 </ a > .   
 {% else %}
  当前用户:
< href ="/profile/user/id={{ user.id }}" > {{ user.username }} </ a > &nbsp;&nbsp;
  
< href ="/auth/logout/"  id ="SignOut" > 注销 </ a > .
 {% endif %}

> 控制结构

 {% for ..%}
 {% endfor %}

 例子:

  < h3 >  最新主题  </ h3 >
  
 {% for topic in last_topic_list %}
  
< li >  {{ topic.birth_date }} |  < href ="{{ topic.get_url }}" > {{ topic.name }} </ a >   </ li >
 {% endfor %}

 顺带了解了一下简单的数据绑定模版语法,比 asp.net 爽多了。

> 注意到绑定的语法是用 {{ 和 }} 定义的:

  {{ topic.name }}

> “管道”语法。在模版中对绑定数据进行格式化,过滤等处理:
{{ comment.comment|escape|urlizetrunc:20|linebreaks }}

又找到其他一些管道的语法例子。其管道函数可能是自定义的,也可能是系统函数。等后面再了解了。这些例子列举如下:

{{ errors|pluralize }}
目前不知道 pluralize 是什么意思,查词义好像是“变成复数形式”。

{{ form.username.errors|join:", " }}
这个用法似乎很巧妙。

{{comment.comment|escape|digest:96}}


> 在看注册表单 /templates/auth/register.htx 的时候,注意到一般的表单字段的书写方式,可以当作例子以后参考。其中包含了验证提示信息,用的是 post back 后验证的模式,目前还未看到如何实现 javascript 在客户端验证的例子:
     < div  class ="FieldControl Mandatory" >
        
< label  for ="id_username" >
            用户名
< span  class ="MandatoryMarker" ></ span >
        
</ label >
 
        {{ form.username }}
        
< div  class ="help" >  
            {% if form.username.errors %} {{ form.username.errors|join:", " }}{% endif %}
        
</ div >
        
<!--  <input type="text" name="username" id="user_username" size="18"    maxlength="18" value="" />  -->
    
</ div >


    
< div  class ="FieldControl Mandatory" >
        
< label  for ="id_password" >
            密码 
< span  cass ="MandatoryMarker" ></ span >
        
</ label >
 
        {{ form.password }}
 
 
        
< div  class ="help" >  
            {% if form.password.errors %} {{ form.password.errors|join:", " }}{% endif %}
        
</ div >
        
<!--  <input type="password" name="password" id="password" size="20" maxlength="100" value=""/>
  
-->
    
</ div >


> 模版中判定相等和不等的语法
  {% ifequal a b %}
  {% endifequal %}

  {% ifnotequal a b %}
  {% endifnotequal %}


> 看到一个类似于 list comprehension 的语法:
{% free_comment_form for essay.Essay essay.id %}


今天学到这里为止了。 除了 /templates/forum/ 和 /templates/test/ 下的内容, 已经看完了其他所有模版。感觉真的很简单易懂,可读性也比 asp.net 程序的页面好!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值