Django之路: 模版篇

一、Django 模版 

上章是介绍简单的把django.http.HttpResponse的内容显示到网页上,下面就介绍以下如何使用渲染模版的方法来显示内容。本节代码是基于Django 1.8,但 Django 1.4 - Django 1.9操作都是一样的。

温馨提示:如果你想学习Django,那么就请您从现在开始按照笔记记录一步一步的用手把代码敲出来,千万不要偷懒哦。。。。。

1、创建一个zqxt_tmpl项目,并创建一个一个learn的应用,

[email protected]:~# django-admin startproject zqxt_tmpl    #项目
[email protected]:~# cd zqxt_tmpl/
[email protected]:~/zqxt_tmpl# python manage.py startapp learn  #应用

2、在把创建的应用加到settings.INSTALLED_APPS中

[email protected]:~/zqxt_tmpl# sudo vi zqxt_tmpl/settings.py
................
INSTALLED_APPS = (
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘learn‘,  #添加的应用
)

3、打开learn/views.py写一个首页视图

[email protected]:~/zqxt_tmpl# sudo vi learn/views.py

from django.shortcuts import render
def home(request):   #创建一个函数调用home.html
        return render(request, ‘home.html‘)

4、在learn目录下新建一个templates文件夹,在里面创建一个home.html

默认配置下,Django的模版系统会知道在app下面找到templates文件夹中的模版文件。

[email protected]:~# mkdir zqxt_tmpl/learn/templates
[email protected]:~# touch zqxt_tmpl/learn/templates/home.html
[email protected]:~# tree zqxt_tmpl
zqxt_tmpl
├── learn
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── home.html
│   ├── tests.py
│   └── views.py
├── manage.py
└── zqxt_tmpl
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    └── wsgi.py

3 directories, 13 files

5、在home.html中写一些内容。

[email protected]:~# sudo vi zqxt_tmpl/learn/templates/home.html

<!DOCTYPE html>
<html>
<head>
        <title>欢迎光临</title>  #title头部欢迎语
</head>
<body>
欢迎您访问吴老二博客,希望在此学习愉快 #内部内容
</body>
</html>

6、将视图函数对应的网址,更改zqxt_tmpl/zqxt_tmpl/urls.py

[email protected]:~# sudo vi zqxt_tmpl/zqxt_tmpl/urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns(‘‘,
    # Examples:
     url(r‘^$‘, ‘learn.views.home‘, name=‘home‘),#把前面的注释去掉
    # url(r‘^blog/‘, include(‘blog.urls‘)),

url(r‘^admin/‘, include(admin.site.urls)),
)

Django 1.8.x及以上:

from django.conf.urls import include, url
from django.contrib import admin
from learn import views as learn_views
 
 
urlpatterns = [
    url(r‘^$‘, learn_views.home, name=‘home‘), #news
    url(r‘^admin/‘, include(admin.site.urls)),
]

7、【可选】创建数据库表

[email protected]:~/zqxt_tmpl# python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django‘s auth system, which means you don‘t have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use ‘root‘): root #数据名称默认root
Email address: 
Password:   #数据库密码
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Django 1.9.x 及上:

[email protected]:~/zqxt_tmpl# python manage.py migrate

创建数据库虽然不会用,但是可以让一些提示消失(提示你要创建数据库之类的)

8、运行开发服务器

[email protected]:~/zqxt_tmpl# python manage.py runserver 192.168.1.30:8001   #如果本地测试的话就不需要加IP了
Validating models...

0 errors found
December 24, 2015 - 09:41:55
Django version 1.6.10, using settings ‘zqxt_tmpl.settings‘
Starting development server at http://192.168.1.30:8001/
Quit the server with CONTROL-C.

网页测试效果:

模版补充知识:

一般的网站通用部分,比如导航,底部,访问统计代码等。

nav.html bottom.html tongji.html

[email protected]:~# cd zqxt_tmpl/learn/templates/
[email protected]:~/zqxt_tmpl/learn/templates# touch nav.html bottom.html tongji.html

可以先写一个base.html来包含这些通用文件(include)

[email protected]:~/zqxt_tmpl/learn/templates# sudo vi base.html

<!DOCTYPE html>
<html>
<head>
        <title>{% block title %}默认标题{% endblock %} -吴老二博客</title>
</head>
<body>
{% include ‘nav.html‘ %}
{% block content %}
<div>这里是默认内容,所有继承都来自这个模版,如果不覆盖就显示这里的默认内容.</div>
{% endblock %}
{% include ‘bottom.html‘ %}
{% include ‘tongji.html‘ %}
</body>
</html>

如果需要,写足够多的block以便继承的模版可以重写该部分,include是包含其它文件的内容,就是把一些网页共用的部分拿出来,重复利用,改动的时候也方便一些,还可以把广告代码放在一个单独的html中,改动也方便,在用到的时候直接继承base.html就好了,继承后的模版也可以在block块中include其它的模版文件。

这里用首页home.html,继承或者说扩展(extends)原来的base.html,可以简单的这样写,重写部分代码(默认值部分不用修改)

[email protected]:~/zqxt_tmpl/learn/templates# sudo vi home.html

<!DOCTYPE html>
<html>
<head>
        <title>欢迎光临</title>
</head>
<body>
{% extends ‘base.html‘ %}
{% block title %}欢迎光临吴老二博客首页{% endblock %}
{% block content %}
{% include ‘ad.html‘ %}
这里是吴老二首页,欢迎光临
{% endblock %}
</body>
</html>

浏览器浏览效果:

注:模版一般放在app下的templates下,Django会自动找到,加入每个app都有一个index.html,当我们在views.py中使用的时候,如何判断是当前app的home.html呢?

这就需要把每个app中的templates文件夹在建一个app的名称,仅和该app相关的模版放在app/templates/app目录下。

例如:项目zqxt下有两个app应用分别为tutorial和tryit

[email protected]:~# django-admin startproject zqxt
[email protected]:~# tree zqxt
zqxt
├── manage.py
└── zqxt
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files
[email protected]:~# cd zqxt 
[email protected]:~/zqxt# python manage.py startapp tutorial
[email protected]:~/zqxt# python manage.py startapp tryit
[email protected]:~/zqxt# cd
[email protected]:~# 
[email protected]:~# tree zqxt
zqxt
├── manage.py
├── tryit
│?? ├── admin.py
│?? ├── __init__.py
│?? ├── models.py
│?? ├── tests.py
│?? └── views.py
├── tutorial
│?? ├── admin.py
│?? ├── __init__.py
│?? ├── models.py
│?? ├── tests.py
│?? └── views.py
└── zqxt
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    └── wsgi.py

3 directories, 17 files
[email protected]:~# cd zqxt/t
tryit/    tutorial/ 
[email protected]:~# cd zqxt/tutorial/
[email protected]:~/zqxt/tutorial# mkdir -p templates/tutorial
[email protected]:~/zqxt/tutorial# touch templates/tutorial/index.html templates/tutorial/search.html
[email protected]:~/zqxt/tutorial# cd ..
[email protected]:~/zqxt# cd t
tryit/    tutorial/ 
[email protected]:~/zqxt# cd tryit/
[email protected]:~/zqxt/tryit# mkdir -p templates/tryit
[email protected]:~/zqxt/tryit# touch templates/tryit/index.html templates/tryit/poll.html
[email protected]:~/zqxt/tryit# cd
[email protected]:~# tree zqxt
zqxt
├── manage.py
├── tryit
│?? ├── admin.py
│?? ├── __init__.py
│?? ├── models.py
│?? ├── templates
│?? │?? └── tryit
│?? │??     ├── index.html
│?? │??     └── poll.html
│?? ├── tests.py
│?? └── views.py
├── tutorial
│?? ├── admin.py
│?? ├── __init__.py
│?? ├── models.py
│?? ├── templates
│?? │?? └── tutorial
│?? │??     ├── index.html
│?? │??     └── search.html
│?? ├── tests.py
│?? └── views.py
└── zqxt
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    └── wsgi.py

7 directories, 21 files

项目zqxt

这在使用的时候“tutorial/index.html”和“tryit/index.html”这样作为app名称的一部分,就不会混淆

二、Django 模版进阶

以下内容主要是针对Django模板中的循环,条件判断,常用的标签,过滤器的使用。

1、列表,字典,类的实例的使用

2、循环:迭代显示列表,字典等中的内容

3、条件判断:判断是否显示该内容,比如判断是手机访问,还是电脑访问,给出不一样的代码。

4、标签:for,if这样的功能都是标签。

5、过滤器:管道符号后面的功能,比如{{ var|length }},求变量长度的length就是一个过滤器。

经常需要将一个或多个变量共享给多个页面或者所有页面使用,比如在网页上显示来访者的IP,这个可以是用Django上下文渲染器来做。

实例,显示一个基本的字符串在页面上(在项目zqxt_tmpl上)

[email protected]:~/zqxt_tmpl/learn# sudo vi views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render
def home(request):
        string = u"我在吴老二博客学习Django,用它来建网站"
        return render(request, ‘home.html‘, {‘string‘: string})

在视图中锄地了一个字符串名称是string到模版home.html,在模版中这样使用:

[email protected]:~/zqxt_tmpl/learn/templates# sudo vi home.html

{{ string }}

浏览器访问,在本机上运行:

[email protected]:~/zqxt_tmpl# python manage.py runserver 192.168.1.30:8000
Validating models...

0 errors found
December 25, 2015 - 03:25:16
Django version 1.6.10, using settings ‘zqxt_tmpl.settings‘
Starting development server at http://192.168.1.30:8000/
Quit the server with CONTROL-C.
[25/Dec/2015 03:25:32] "GET / HTTP/1.1" 200 53

浏览器:

实例二:for循环和list内容的显示在zqxt_tmpl/learn/views.py

[email protected]:~/zqxt_tmpl# sudo vi learn/views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render
def home(request):
        TutorialList = ["HTML", "CSS", "jQuery", "Django"]
        return render(request, ‘home.html‘, {‘TutorialList‘: TutorialList})

在视图中传递了一个List到模版zqxt_tmpl/learn/templates/home.html,在模版中使用它:

[email protected]:~# sudo vi zqxt_tmpl/learn/templates/home.html

教程列表:
{% for i in TutorialList %}
{{ i }}
{% endfor %}

本地运行服务:

[email protected]:~/zqxt_tmpl# python manage.py runserver 192.168.1.30:8000
Validating models...

0 errors found
December 25, 2015 - 03:38:06
Django version 1.6.10, using settings ‘zqxt_tmpl.settings‘
Starting development server at http://192.168.1.30:8000/
Quit the server with CONTROL-C.
[25/Dec/2015 03:38:10] "GET / HTTP/1.1" 200 42

for循环要一个结束标记,上面的代码假如对应的是首页的网址(自己修改urls.py),显示在网页上就是:

总结:一般的变量之类的用{{}} (变量),功能类的,比如循环,条件判断是用{%     %}(标签)

实例三:显示字典中的内容:

[email protected]:~/zqxt_tmpl# sudo vi learn/views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render
def home(request):
        info_dict = {‘site‘: u‘吴老二‘, ‘content‘: u‘自己建网站‘}
#       TutorialList = ["HTML", "CSS", "jQuery", "Django"]
        return render(request, ‘home.html‘, {‘info_dict‘: info_dict})
#       return render(request, ‘home.html‘, {‘TutorialList‘: TutorialList})

为了和上个例子做对比我把上例注释掉了,您也可以注释掉也可以直接删除掉。

[email protected]:~/zqxt_tmpl# sudo vi learn/templates/home.html

【站点】:{{ info_dict.site }} 【内容】:{{ info_dict.content }}
#{% for i in TutorialList %}
#{{ i }}
#{% endfor %}

在模版注释了上个实例您也可以删除,这个实例模版中取字典的键是用info_dict.site,而不是Python中的info_dict[‘site‘],效果如下:

注:以上实例在浏览器浏览之前都要运行以下python manage.py runserver IP:prot

还可以这样遍历字典:

[email protected]:~/zqxt_tmpl# sudo vi learn/templates/home.html

{% for key, value in info_dict.items %}
        {{ key }}: {{ value }}
{% endfor %}

其实就是遍历这样一个List:[(‘content‘: u‘自己建网站‘), (‘site‘: u‘吴老二‘)]

实例四:在模版进行条件判断和for循环的详细操作:

[email protected]:~/zqxt_tmpl# sudo vi learn/views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render
def home(request):
        List = map(str, range(100)) #一个长度为100的list
        return render(request, ‘home.html‘, {‘List‘: List})

使用逗号将这些元素连接起来:

[email protected]:~/zqxt_tmpl# sudo vi learn/templates/home.html

{% for item in List %}
        {{ item }},
{% endfor %}

效果如下:

最后一个元素的后面也有一个逗号,这样不是我们想要的,如何判断是不是遍历到最后一个元素呢?

这里可以用forloop.last这个变量,如果是最后一项其为真,否则为假,更改如下:

[email protected]:~/zqxt_tmpl# sudo vi learn/templates/home.html

{% for item in List %}
        {{ item }}{% if not forloop.last %},{% endif %} 
{% endfor %}

for循环补充如下:

变量描述

forloop.counter 索引从1

forloop.counter0 索引从0

forloop.revcounter 索引从最大长度到1

forloop.revcounter0 索引从最大长度到0

forloop.first 当遍历的元素为第一项时为真

forloop.last 当遍历的元素为最后一项时为真

forloop.parentloop 用在嵌套的for循环中,获取上一层for循环的forloop

当列表中可能为空值时用for empty

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>抱歉,列表为空</li>
{% endfor %}
</ul>

实例五,模版上得到视图对应的网址:

# views.py
def add(request, a, b):
    c = int(a) + int(b)
    return HttpResponse(str(c))
 
 
# urls.py
urlpatterns = patterns(‘‘,
    url(r‘^add/(\d+)/(\d+)/$‘, ‘app.views.add‘, name=‘add‘),
)
 
 
# template html
{% url ‘add‘ 4 5 %}

这样网址上就会显示出:/add/4/5/这个网址,假如我们以后修改urls.py中的

r‘^add/(\d+)/(\d+)/$‘

这一部分,改成另的,比如:

r‘^jiafa/(\d+)/(\d+)/$‘

这样,我们不需要在次修改模版,当再次访问的时候,网址就会自动变成/jiafa/4/5/

注意:如果是Django 1.4的话,需要在模版开头上  {% load url from future %} (如果有 extends 的话,加在 extends 下面)

这可以使用as语句将内容取别名(相当于定义一个变量),多次使用(但视图名称到网址转换只进行了一次)

{% url ‘some-url-name‘ arg arg2 as the_url %}
 
<a href="{{ the_url }}">链接到:{{ the_url }}</a>

实例六,模版中的逻辑操作

==,!=,>=,<=,>,<这些比较都可以在模版中使用,比如:

{% if var >= 90 %}
成绩优秀,吴老二博客你没少去吧!学得不错
{% elif var >= 80 %}
成绩良好
{% elif var >= 70 %}
成绩一般
{% elif var >= 60 %}
需要努力
{% else %}
不及格啊,大哥!多去吴老二博客学习啊!
{% endif %}

and,or,not,in,not in也可以在模版中使用

假如我们判断num是不是在0到100之间:

{% if num <= 100 and num >= 0 %}
num在0到100之间
{% else %}
数值不在范围之内!
{% endif %}

假如我们判断’wulaoer‘

{% if ‘wulaoer‘ in List %}
自强学堂在名单中
{% endif %}

实例七,模版中获取当前网址,当前用户等:

如果不是在 views.py 中用的 render 函数,是render to response的话,需要将request加入到上下文渲染器

Django 1.8 及以后 修改settings.py

TEMPLATES = [
    {
        ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,
        ‘DIRS‘: [],
        ‘APP_DIRS‘: True,
        ‘OPTIONS‘: {
            ‘context_processors‘: [
                ...
                ‘django.template.context_processors.request‘,
                ...
            ],
        },
    },
]

Django 1.7 及以前修改settings.py:

如果没有TEMPLATE_CONTEXT_PROCESSORS请自行添加下列默认值:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
)

然后再加上 django.core.context_processors.request

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "django.core.context_processors.request",
    ...
)

然后在模版中我们就可以用request了。

获取当前用户:

{{ request.user }}

如果登陆就显示内容,不登陆就不显示内容:

{% if request.user.is_authenticated %}
    {{ request.user.username }},您好!
{% else %}
    请登陆,这里放登陆链接
{% endif %}

获取当前网址:

{{ request.path }}

获取当前GET参数:

{{ request.GET.urlencode }}

合并到一起用的一个例子:

<a href="{{ request.path }}?{{ request.GET.urlencode }}&delete=1">当前网址加参数 delete</a>

比如我们可以判断delete参数是不是1来删除当前的页面内容。

时间: 2024-08-08 01:15:03

Django之路: 模版篇的相关文章

Django之路--第一篇

1.安装django pip install django 2.创建django工程 django-admin startproject 工程名 3.创建APP python manage.py startapp cmdb 4.project.settings.py配置静态文件 css/js 最后面添加 STATICFILES_DIRS=( os.path.join(BASE_DIR,'static'), ) 5.模板路径 [os.path.join(BASE_DIR,'templates'),

Django之路:安装与配置

Django之路:安装与配置 MTV Model Template View 数据库 模版文件 业务处理 了解Django框架,功能齐全 一.安装Django&Django基本配置 安装Django pip3 django 配置Django 1.配置Django环境变量 D:\Program files\python37 D:\Program files\python37\Lib\site-packages\django\bin D:\Program files\python37\Scripts

Django学习之配置篇

Django之路:安装与配置 MTV Model Template View 数据库 模版文件 业务处理 了解Django框架,功能齐全 一.安装Django&Django基本配置 安装Django pip3 django 配置Django 1.配置Django环境变量 D:\Program files\python37 D:\Program files\python37\Lib\site-packages\django\bin D:\Program files\python37\Scripts

django -- url (模版语言 {% url &#39;test1&#39; param1=5 param2=6 %})

如果想让form表单提交的url是类似 action="/index-5-6.html" 这样的,可以在html模版语言中使用{% url 'test1' param1=5 param2=6 %} urls.py from django.conf.urls import url, include from mytest import views urlpatterns = [ url(r'^index-(?P<param1>\d+)-(?P<param2>\d+

Django内建模版标签和过滤器

第四章列出了许多的常用内建模板标签和过滤器.然而,Django自带了更多的内建模板标签及过滤器.这章附录列出了截止到编写本书时,Django所包含的各个内建模板标签和过滤器,但是,新的标签是会被定期地加入的. 对于提供的标签和过滤器,最好的参考就是直接进入你的管理界面.Django的管理界面包含了一份针对当前站点的所有标签和过滤器的完整参考.想看到它的话,进入你的管理界面,单击右上角的Documentation(文档)链接. 内建文档中的“标签和过滤器”小节阐述了所有内建标签(事实上,本附录中的

(转)Android项目重构之路:实现篇

前两篇文章Android项目重构之路:架构篇和Android项目重构之路:界面篇已经讲了我的项目开始搭建时的架构设计和界面设计,这篇就讲讲具体怎么实现的,以实现最小化可用产品(MVP)的目标,用最简单的方式来搭建架构和实现代码. IDE采用Android Studio,Demo实现的功能为用户注册.登录和展示一个券列表,数据采用我们现有项目的测试数据,接口也是我们项目中的测试接口. 项目搭建 根据架构篇所讲的,将项目分为了四个层级:模型层.接口层.核心层.界面层.四个层级之间的关系如下图所示:

(转)Android项目重构之路:界面篇

在前一篇文章<Android项目重构之路:架构篇>中已经简单说明了项目的架构,将项目分为了四个层级:模型层.接口层.核心层.界面层.其中,最上层的界面,是变化最频繁的一个层面,也是最复杂最容易出问题的一个层面,如果规划不好,很容易做着做着,又乱成一团了. 要规划好界面层,至少应该遵循几条基本的原则: 保持规范性:定义好开发规范,包括书写规范.命名规范.注释规范等,并按照规范严格执行: 保持单一性:布局就只做布局,内容就只做内容,各自分离好:每个方法.每个类,也只做一件事情: 保持简洁性:保持代

Django框架之第二篇

Django框架之第二篇 一.知识点回顾 1.MTV模型 model:模型,和数据库相关的 template:模板,存放html文件,模板语法(目的是将变量如何巧妙的嵌入到HTML页面中). views:视图函数 另加urls:url路径与视图函数的映射关系,,可以不是一一对应的. 2.相关的一些命令 创建一个Django项目:django-admin  startproject  projectname 创建一个项目下的应用:python3  manage.py  startapp  appn

Linux运维之路 基础篇:Linux基础命令(一)

Linux运维之路 基础篇:Linux基础命令(一) Linux哲学宗旨: 一切皆文件:把几乎所有的资源,包括硬件设备都组织为文件 有众多单一的小程序组成,一个程序制实现一个功能,组成小程序完成复杂操作 尽量避免和用户交互:实现脚本编程,以自动完成某些功能 使用纯文本文件保存配置信息 终端:用户和主机交互时用到的设备 物理终端:直接接入的设备也叫控制台/dev/console 虚拟终端:附加在物理终端上虚拟出的,默认启动六个,Ctrl+Alt(F1~F6),系统启动时,默认启动虚拟终端1,启动终