因为将来有一个 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 %}
> 块可以嵌套
常见的做法是在父模版里面预先定义好一个块,在子模版里面具体写块的内容。
> 控制结构
{% else %}
{% endif %}
{% if variable %}
{% else %}
{% endif %}
{% else %}
{% endif %}
这里 if 可以是 if not.
> user 好像是一个预定义对象,类似于 asp.net 中的 User 对象。一个典型的代码如下:
{% if user.is_anonymous %}
您还没有登陆. 请
< a href ="/auth/login/" id ="SignIn" > 登陆 </ a >
或
< a href ="/auth/register/" id ="Register" > 注册 </ a > .
{% else %}
当前用户: < a href ="/profile/user/id={{ user.id }}" > {{ user.username }} </ a >
< a href ="/auth/logout/" id ="SignOut" > 注销 </ a > .
{% endif %}
您还没有登陆. 请
< a href ="/auth/login/" id ="SignIn" > 登陆 </ a >
或
< a href ="/auth/register/" id ="Register" > 注册 </ a > .
{% else %}
当前用户: < a href ="/profile/user/id={{ user.id }}" > {{ user.username }} </ a >
< a href ="/auth/logout/" id ="SignOut" > 注销 </ a > .
{% endif %}
> 控制结构
{% for ..%}
{% endfor %}
{% endfor %}
例子:
<
h3
>
最新主题
</
h3
>
{% for topic in last_topic_list %}
< li > {{ topic.birth_date }} | < a href ="{{ topic.get_url }}" > {{ topic.name }} </ a > </ li >
{% endfor %}
{% for topic in last_topic_list %}
< li > {{ topic.birth_date }} | < a 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 >
< 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 %}
{% endifequal %}
{% ifnotequal a b %}
{% endifnotequal %}
> 看到一个类似于 list comprehension 的语法:
{% free_comment_form for essay.Essay essay.id %}
今天学到这里为止了。