Django 模板进阶
本节主要讲Django模板中的循环,条件判断,常用的标签,过滤器的使用:
1.列表,字典,类的实例的使用
2.循环:迭代显示列表,字典等中的内容
3.条件判断: 判断是否显示该内容,比如判断是手机访问,还是电脑访问,给出不一样的代码。
如果需要将一个或多个变量共享给多个网页或者所有网页使用,比如在网页上显示来访者的IP,
这个可以使用 Django 上下文渲染器 来做。
实例一,显示一个基本的字符串在网页上
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,在模板中这样使用它:
def home(request):
string = u"我在自强学堂学习Django,用它来建网站"
return render(request, 'home.html', {'string': string})
home.html
1
{{ string }}
实例二,讲解了基本的for循环和List内容的显示
node2:/app/zqxt_tmpl/learn#cat views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
def home(request):
TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
return render(request, 'home.html', {'TutorialList': TutorialList})
node2:/app/zqxt_tmpl/learn#
node2:/app/zqxt_tmpl/learn/templates#cat home.html
教程列表:
{% for i in TutorialList %}
{{ i }}
{% endfor %}
html 换行
node2:/app/zqxt_tmpl/learn/templates#cat home.html
教程列表:
<br/>
{% for i in TutorialList %}
{{ i }}
<br/>
{% endfor %}
实例三,显示字典中的内容:
node2:/app/zqxt_tmpl/learn/templates#cat ../views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
def home(request):
info_dict = {'site': u'自强学堂', 'content': u'各种IT技术教程'}
return render(request, 'home.html', {'info_dict': info_dict})
node2:/app/zqxt_tmpl/learn/templates#
node2:/app/zqxt_tmpl/learn/templates#cat home.html
{{info_dict}}
遍历字典:
还可以这样遍历字典:
{% for key, value in info_dict.items %}
{{ key }}: {{ value }}
{% endfor %}
实例四,在模板进行条件判断和for循环的详细操作:
node2:/app/zqxt_tmpl/learn/templates#cat ../views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
def home(request):
List = map(str, range(100))# 一个长度为100的 List
return render(request, 'home.html', {'List': List})
http://192.168.137.3:9000/learn/
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
我们会发现最后一个元素后面也有一个逗号,这样肯定不爽,如果判断是不是遍历到最后一个元素了呢?
{% for item in List %}
{{ item }}{% if not forloop.last %},{% endif %}
{% endfor %}
实例五,模板上得到视图对应的网址:
node2:/app/zqxt_tmpl/learn/templates#cat ../views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
def home(request):
List = map(str, range(100))# 一个长度为100的 List
return render(request, 'home.html', {'List': List})
def add(request, a, b):
c = int(a) + int(b)
return HttpResponse(str(c))
node2:/app/zqxt_tmpl/learn/templates#cat /app/zqxt_tmpl/zqxt_tmpl/urls.py
"""zqxt_tmpl URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from learn import views as learnview
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^learn/', learnview.home),
url(r'^add/(d+)/(d+)/$', 'learnview.add', name='add'),
]
报错:
File "/usr/local/python27/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^learn/', learnview.home),
url(r'^add/(d+)/(d+)/$', learnview.add, name='add'),
]
def add(request, a, b):
c = int(a) + int(b)
return render(request, 'home.html', {'c': c})
node2:/app/zqxt_tmpl/learn#cat templates/home.html
<h1>----<h1/>
{{c}}
{% url 'add' 4 5 %}
实例6,模板中的逻辑操作: