博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django开发框架-view & template
阅读量:4500 次
发布时间:2019-06-08

本文共 2237 字,大约阅读时间需要 7 分钟。

django框架的主要模型是MVT,Model模型,View视图,Template模板,基于基本的HttpRequest方式。

django支持的数据库有四种:PostgreSQL,MySQL, Oracle,SQLite3。(所以目前应该把精力放在PostgreSQL上)

1.新建工程:django-admin.py startproject mysite,新建的mysite的目录下有四个文件:__init__.py, manage.py, settings.py, urls.py 。

2.运行开发服务器:python manage.py runserver。(这是内建的、轻量的web服务)(如何非本地IP访问?)

3.

4.

5.

6.

7.

8.

9.(忘记保存了,我草)

10.模板的深度变量查找:使用句点符号 . 。据点查找规则:字典类型查找,属性查找,方法调用,列表类型索引查找。设置函数属性function.alters_data = true ,在模板载入时,方法就不会执行。如果变量不存在,就处理成空字符串。context对象可以像字典一样insert, delete。

11.模块标签:if/else {% if bool %} ... {% else %} ... {% endif %}, if标签不允许在同一个标签中使用 and 和 or ,不支持用圆括号来组合比较操作。for {% for athlete in athlete_list %} ... {% endfor %} ,for 标签还有一个可选的 {% empty %}标签,当列表为空时使用。有一个forloop的模板变量。ifequal/ifnotequal 标签,只有模板变量,字符串,整数和小数可以作为该标签的参数。注释 {# comment #} ,多行注释 {% comment %} ... {% endcomment %}。 过滤器使用管道字符,可以套接,过滤器参数跟随冒号之后并且总是用双引号包含。

1 def current_time(request):                                                  |   ~                          2  10     now = datetime.datetime.now()                                           |   ~                          3  11     t = get_template('mytemplate.html')                                     |   ~                          4  12     html = t.render(Context({
'current_date': now})) | ~ 5 13 return HttpResponse(html)

12.先在settings.py中设置template_dirs,然后编写html代码,再到views.py中编写对应的视图代码。

1 def current_time(request):2     now = datetime.datetime.now()3     return render_to_response('mytemplate.html', {
'current_date': now})

另一种写法:使用python的内建函数locals()

def current_time(request):    current_date = datetime.datetime.now()    return render_to_response('mytemplate.html', locals())

get_template()函数可以使用子目录。

13.include模板标签。模板继承,代码示例:

base.html:

1  2  3  4      {% block title %}{% endblock %}  5  6  7     

My helpful timestamp site

8 {% block content %}{% endblock %} 9 {% block footer %}10

11

Thank for visiting my site.

12 {% endblock %}13 14

current_date.html:

1 {% extends "base.html" %}2 3 4 {%block title%}The current time{% endblock %}5 6 {%block content%}7 

It is now {

{ current_date }}.

8 {% endblock %}

简单的视图模式和模板模式介绍完毕。

 

转载于:https://www.cnblogs.com/nuaalida/p/4338244.html

你可能感兴趣的文章
为Linux配置常用源:epel和IUS
查看>>
天府地
查看>>
C#高级编程
查看>>
JS实现从照片中裁切自已的肖像
查看>>
使用 https://git.io 缩短 a GitHub.com URL.
查看>>
拷贝、浅拷贝、深拷贝解答
查看>>
NS3 实验脚本的编写步骤
查看>>
四元数
查看>>
【Linux】Linux查看程序端口占用情况
查看>>
微软职位内部推荐-Software Development Engineer
查看>>
Git常用命令
查看>>
Windows 2003+IIS6+PHP5.4.10配置PHP支持空间的方法(转)
查看>>
去除express.js 3.5中报connect.multipart() will be removed in connect 3.0的警告(转)
查看>>
Android WIFI 无缝切换 小结(1)
查看>>
BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)
查看>>
BS系统开发历程
查看>>
asp.net 设置回车的默认按钮 (转载)
查看>>
Palindrome Partitioning
查看>>
Microservice架构模式简介
查看>>
换种形式工作
查看>>