您的位置:首页 > 编程语言 > Go语言

django学习笔记4:模版

2016-06-30 22:02 387 查看
django模板
先在test1中添加一个templates目录用来放模板
修改mytest\test1\views.py
#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse

def index(request):
return HttpResponse("hello world 你好")

def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))

def add2(request, a, b):
c = int(a) + int(b)
return HttpResponse(str(c))

def old_add2(request,a,b):
return HttpResponseRedirect(
reverse('add2',args=(a,b))
)

def home(request):
return render(request,'home.html')
render( )加载模板
然后在mytest\test1\templates目录下创建一个home.html
目录结构如下:
mytest
|----manage.py
|----mytest
       |----__init__.py
       |----settings.py
       |----urls.py
       |----wsgi.py
|----test1
       |----__init__.py
       |----admin.py
       |----apps.py
       |----models.py
       |----tests.py
       |----views.py
       |----migrations
              |----__init__.py
       |----templates
              |----home.html

修改mytest\test1\templates\home.html
<html>
<head>
<title>hello</title>
</head>
<body>
hello wolrd 你好!!!!!!
</body>
</html>

修改mytest\test1\urls.py

from django.conf.urls import url
from django.contrib import admin
from test1 import views as test1_views

urlpatterns = [
url(r'^$',test1_views.index),
url(r'^admin/', admin.site.urls),
url(r'^add/$',test1_views.add,name='add'),
url(r'^add2/(\d+)/(\d+)/$', test1_views.old_add2),
url(r'^new_add2/(\d+)/(\d+)/$',test1_views.add2,name='add2'),
url(r'^home/$',test1_views.home,name='home')
]

启动服务,输入127.0.0.1:8000/home/



模板的复用与继承
复用
在mytest\test1\templates\下新建一个base.html
修改mytest\test1\templates\base.html
<head>
<title>hello111</title>
</head>

修改 mytest\test1\templates\home.html
<html>
{% include 'base.html' %}
<body>
hello wolrd 你好!!!!!!
</body>
</html>
include 引入模板



继承
修改mytest\test1\templates\base.html
<head>
<title>{% block title %}hahaha{% endblock %}</title>
</head>
<body>
{% block content %}
这里是原来内容 (如果继承的模板不修改就回显示这个)
{% endblock %}
</body>
block title是标题块,每个块都要有结束块
block content是内容块

在mytest\test1\templates\下新建一个jcheng.html
内容如下:
{% extends 'base.html' %}
{% block title %}这是继承的{% endblock %}
{% block content %}
这里是新内容
{% endblock %}
extends为继承模板

修改
mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
hello wolrd 你好!!!!!!
</body>
</html>


浏览器http://127.0.0.1:8000/home/



注意:模板一般放在app下的templates中,Django会自动去这个文件夹中找。但 假如我们每个app的templates中都有一个 index.html,当我们在views.py中使用的时候,直接写一个 render(request, 'index.html'),Django 能不能找到当前 app 的 templates 文件夹中的
index.html 文件夹呢?(答案是不一定能,有可能找错)

Django 模板查找机制: Django 查找模板的过程是在每个 app 的 templates 文件夹中找(而不只是当前 app 中的代码只在当前的 app 的 templates 文件夹中找)。各个 app 的 templates 形成一个文件夹列表,Django 遍历这个列表,一个个文件夹进行查找,当在某一个文件夹找到的时候就停止,所有的都遍历完了还找不到指定的模板的时候就是
Template Not Found (过程类似于Python找包)。这样设计有利当然也有弊,有利是的地方是一个app可以用另一个app的模板文件,弊是有可能会找错了。所以我们使用的时候在 templates 中建立一个 app 同名的文件夹,这样就好了。

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

这样,使用的时候,模板就是 "app_name/index.html" 这样有app_name作为名称的一部分,就不会混淆。

Django模板进阶
给模板传参
修改mytest\test1\views.py
#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse

def index(request):
return HttpResponse("hello world 你好")

def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))

def add2(request, a, b):
c = int(a) + int(b)
return HttpResponse(str(c))

def old_add2(request,a,b):
return HttpResponseRedirect(
reverse('add2',args=(a,b))
)

def home(request):
stringa = u'你好啊!!!'
return render(request,'home.html',{'stringa':stringa})
定义一个参数,并传进模板中

修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{{stringa}}
<br>
hello wolrd 你好!!!!!!
</body>
</html>
在模板中引入参数



模板中循环
修改mytest\test1\views.py
#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse

def index(request):
return HttpResponse("hello world 你好")

def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))

def add2(request, a, b):
c = int(a) + int(b)
return HttpResponse(str(c))

def old_add2(request,a,b):
return HttpResponseRedirect(
reverse('add2',args=(a,b))
)

def home(request):
lista = ['a','b','c','d']
return render(request,'home.html',{'lista':lista})


修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{% for i in lista %}
{{i}},
{% endfor %}
<br>
hello wolrd 你好!!!!!!
</body>
</html>
模板中for循环,{% for %}{% endfor%}
感觉Django的模板和flask很像,难道也是jinjia2????哈哈哈



上面的循环有个不美观的地方就是最后多了一个逗号,给这个for加一个判断
修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{% for i in lista %}
{{i}}
{% if not forloop.last %}
,
{% endif %}
{% endfor %}
<br>
hello wolrd 你好!!!!!!
</body>
</html>
forloop.last是判断是否是最后一项





模板中的URL尽量用 {% url 'add' 4 5 %} 这种形式,这个是跟着视图函数的,不会随着url.py里的正则变化而失效

判断列表是否为空
修改mytest\test1\views.py
#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse

def index(request):
return HttpResponse("hello world 你好")

def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))

def add2(request, a, b):
c = int(a) + int(b)
return HttpResponse(str(c))

def old_add2(request,a,b):
return HttpResponseRedirect(
reverse('add2',args=(a,b))
)

def home(request):
lista = []
return render(request,'home.html',{'lista':lista})


修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{% for i in lista %}
{{i}}
{% if not forloop.last %}
,
{% endif %}
{% empty %}
<li>列表为空</li>
{% endfor %}
<br>
hello wolrd 你好!!!!!!
</body>
</html>



模板中获得当前URL,当前用户的那个信息
修改mytest\mytest\settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

教程说要添加这一句,'django.template.context_processors.request',但是我发现settings.py里已经存在

修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{% for i in lista %}
{{i}}
{% if not forloop.last %}
,
{% endif %}
{% empty %}
<li>列表为空</li>
{% endfor %}
<br>
当前的URL:{{request.path}}
<br>
hello wolrd 你好!!!!!!
</body>
</html>
request.path是当前路径
request.user 是登入后的用户信息,request.user.username是用户名



也可以获得GET的参数
修改mytest\test1\templates\home.html
<html>
{% include 'jcheng.html' %}
<body>
{% for i in lista %}
{{i}}
{% if not forloop.last %}
,
{% endif %}
{% empty %}
<li>列表为空</li>
{% endfor %}
<br>
当前的URL:{{request.path}}
<br>
获取当前GET参数:{{ request.GET.urlencode }}
<br>
hello wolrd 你好!!!!!!
</body>
</html>
request.GET.urlencode获取get参数

输入url:127.0.0.1:8000/home/?a=1

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: