创建一个项目bcxt_tmpl
创建一个应用learn
配置url
i.在应用下创建一个urls.py模块
i.把我们应用创建的urls.py引入到项目里(做好关联)
url(r'^learn/', include(learn.urls))
视图
i.首先应该先创建HTML页面
默认配置下,Django 的模板系统会自动找到app下面的templates文件夹中的模板文件。
i.创建视图
def home(request):
return render(request, 'home.html')
i.配置url
from django.conf.urls import urlfrom learn import views
urlpatterns = [
url(r'^home/$', views.home)
]
i.跑起django项目
python manage.py runserver
i.在浏览器输入
http://127.0.0.1:8000/learn/home/
注意:render 是返回模板渲染
模板
显示一个基本的字符串在网页上
步骤1(思路:在视图里面定义):
from django.shortcuts import render
Create your views here.def home(request):
string = '欢迎来到北财学堂'
return render(request, 'home.html', {'string': string})
from django.shortcuts import render
Create your views here.def home(request):
string = '欢迎来到北财学堂'
context = {'string': string}
return render(request, 'home.html', context)
步骤2:在HTML里面渲染 {{ }}
在home.html里面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
</head>
<body>
<h1>{{ string }}</h1>
</body>
</html>
基本的 for 循环 和 List内容的显示
简单总结一下:一般的变量之类的用 {{ }}(变量),功能类的,比如循环,条件判断是用 {% %}(标签)
i.在视图函数里面定义一个列表给模板传过去
from django.shortcuts import render
Create your views here.def home(request):
alist = ['python全栈+人工智能', '大数据技术', 'HTML5', 'UI设计']
context = {'a': alist}
return render(request, 'home.html', context)
i.在模板里面操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
</head>
<body>
{% for xxxxx in a %}
<h1>{{ xxxxx }}</h1>
{% endfor %}
</body>
</html>
显示字典中内容
步骤1 在views.py里面配置内容
from django.shortcuts import render
Create your views here.def home(request):
adict = {'one': '蒙多', 'two': '猪妹'}
context = {'a': adict}
return render(request, 'home.html', context)
步骤2:在模板里面获取内容并展示
调用字典的值可以用 键名(context)+键名(adict的键)
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>home</title></head><body><h1>one:{{ a.one }}</h1><h2>two:{{ a.two }}</h2></body></html>
在模板进行 条件判断和 for 循环的详细操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
</head>
<body>
{% for item in List %}
{{ item }}{% if not forloop.last %},{% endif %}
{% endfor %}
</body>
</html>
from django.shortcuts import render
Create your views here.def home(request):
List = map(str, range(100)) # 一个长度为100的 List
return render(request, 'home.html', {'List': List})
网页跳转
urls.py
from django.conf.urls import urlfrom learn import views
urlpatterns = [
url(r'^home/$', views.home),
url(r'^detail/$', views.detail, name='deat')
]
views.py
def detail(request):
return render(request, 'detail.html')
最关键的一步,我们如何在网页中进行跳转,利用模板语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
</head>
<body>
<a href="{% url 'deat' %}">跳转</a>
</body>
</html>
调用对象的方法
步骤:
- 新建一个项目test4、应用booktest
- 新建应用urls、然后配置项目urls
from django.conf.urls import include, urlfrom django.contrib import adminfrom booktest import urls as booktest_urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('booktest.urls', namespace='booktest'))
]
- 定义模型类
from django.db import models
Create your models here.class BookInfo(models.Model):
btitle = models.CharField(max_length=20)
# db_column更改在数据库里面显示的字段名
bpub_date = models.DateTimeField(db_column='pub_date')
bread = models.IntegerField()
bcommet = models.IntegerField()
isDelete = models.BooleanField()
class Meta():
db_table = 'bookinfo'
class HeroInfo(models.Model):
hname = models.CharField(max_length=20)
hgender = models.BooleanField()
hcontent = models.CharField(max_length=10000)
isDelete = models.BooleanField()
# 'BookInfo' 的引号问题,如果不加引号也行,前提是之前有定义BookInfo这个类,如果没定义也想外键 就必须加引号
book = models.ForeignKey('BookInfo')
- 编写一个index视图
from django.shortcuts import renderfrom booktest.models import *
Create your views here.def index(request):
hero = HeroInfo.objects.get(pk=1)
context = {'hero': hero}
return render(request, 'index.html', context)
- 配置应用urls.py
from django.conf.urls import urlfrom booktest import views
urlpatterns = [
url(r'^$', views.index,name='index')
]
- index.html配置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ hero.hname }}
</body>
</html>
注意: 以上,我们在HTML里面是通过对象.属性进行调用
7.另外一种方式调用对象
1.在models.py中我们增加一个方法showname
from django.db import models
Create your models here.class BookInfo(models.Model):
btitle = models.CharField(max_length=20)
# db_column更改在数据库里面显示的字段名
bpub_date = models.DateTimeField(db_column='pub_date')
bread = models.IntegerField()
bcommet = models.IntegerField()
isDelete = models.BooleanField()
class Meta():
db_table = 'bookinfo'
class HeroInfo(models.Model):
hname = models.CharField(max_length=20)
hgender = models.BooleanField()
hcontent = models.CharField(max_length=10000)
isDelete = models.BooleanField()
# 'BookInfo' 的引号问题,如果不加引号也行,前提是之前有定义BookInfo这个类,如果没定义也想外键 就必须加引号
book = models.ForeignKey('BookInfo')
def showname(self):
return self.hname
1.在html里面我们可以调用{{hero.showname}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ hero.showname }}
</body>
</html>
8.定义模板
1.在view.py中
from django.shortcuts import renderfrom booktest.models import *
Create your views here.def index(request):
# hero = HeroInfo.objects.get(pk=1)
# context = {'hero': hero}
list = HeroInfo.objects.all()
context = {'list': list}
return render(request, 'index.html', context)
1.在html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ hero.showname }}
{% for hero in list %}
<li>{{ forloop.counter }}:{{ hero.showname }}</li>
{% empty %}
啥也没有找到
{% endfor %}
</body>
</html>
其中:
{% for hero in list %}
如果有数据走这个分支
{% empty %}
如果没数据走这个分支
{% endfor %}
{{ forloop.counter }} # 表示循环的第几次
反向解析
步骤
- 创建一个show.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ id }}</h1>
</body>
</html> - 定义一个视图show
from django.shortcuts import renderfrom booktest.models import *
Create your views here.def index(request):
# hero = HeroInfo.objects.get(pk=1)
# context = {'hero': hero}
list = HeroInfo.objects.all()
context = {'list': list}
return render(request, 'index.html', context)
def show(request, id):
context = {'id': id}
return render(request, 'show.html', context)
- 配置url
from django.conf.urls import urlfrom booktest import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'(\d+)', views.show, name='show')
]
- 在index.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="123">展示连接1</a>
{{ hero.showname }}
{% for hero in list %}
<li>{{ forloop.counter }}:{{ hero.showname }}</li>
{% empty %}
啥也没有找到
{% endfor %}
</body>
</html> - 将项目跑起来,在index页面中点击链接
- 在index中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{% url 'booktest:show' %}">展示连接1</a>
{{ hero.showname }}
{% for hero in list %}
<li>{{ forloop.counter }}:{{ hero.showname }}</li>
{% empty %}
啥也没有找到
{% endfor %}
</body>
</html> - 可以带参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{% url 'booktest:show' 456 %}">展示连接1</a>
{{ hero.showname }}
{% for hero in list %}
<li>{{ forloop.counter }}:{{ hero.showname }}</li>
{% empty %}
啥也没有找到
{% endfor %}
</body>
</html>
模板继承
步骤一(模板继承) - 创建一个base.html 和 index_2.html
- 在base.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{% block head %}{% endblock %}
</head>
<body>
<h1>logo</h1>
{% block content1 %}
{% endblock %}
<h1>contact</h1>
</body>
</html>
- 在index_2中,先删除所有HTML内容,剩下一个空白的HTML文档
继承语法: {% extends 'base.html' %}
{% extends 'base.html' %} - 写一个临时用于练习的模板继承的视图
def index2(request):
return render(request, 'index_2.html') - 配置urls
from django.conf.urls import urlfrom booktest import views
urlpatterns = [
url(r'^$', views.index, name='index'),
# url(r'(\d+)', views.show, name='show'),
url(r'^index2/$', views.index2, name='index2'),
]
- 跑起django项目
python manage.py runserver
7.在index_2.html中
{% extends 'base.html' %}
{% block content1%}
<h1>新填充的内容</h1>
{% endblock %}
三层继承结构
举个栗子(如图)
因为之前已经创建了base.html,我们现在只需要创建base_user.html和base_goods.html
因为base_user.html继承自base.html,所以在base_user中
{% extends 'base.html' %}同理,在base_goods.html中
{% extends 'base.html' %}在base_user.html中
{% extends 'base.html' %}
{% block content1 %}
<table border="1">
<tr>
<td height="300">用户导航</td>
<td>{% block user_content %}{% endblock %}</td>
</tr>
</table>
{% endblock %}
6.添加新的user模板,譬如创建user1.html和user2.html,均继承自base_user.html
{% extends 'base_user.html' %}
- 在user1.html中
{% extends 'base_user.html' %}
{% block user_content %}
<h2>用户中心1</h2>
{% endblock user_content%} - 在user2.html中
{% extends 'base_user.html' %}
{% block user_content %}
<h2>用户中心2</h2>
{% endblock user_content%}
9.编写user1和user2的视图
def index2(request):
return render(request, 'index_2.html')
def user1(request):
return render(request, 'user1.html')
def user2(request):
return render(request, 'user2.html')
- 配置url
from django.conf.urls import urlfrom booktest import views
urlpatterns = [
url(r'^$', views.index, name='index'),
# url(r'(\d+)', views.show, name='show'),
url(r'^index2/$', views.index2, name='index2'),
url(r'^user1/$', views.user1, name='user1'),
url(r'^user2/$', views.user2, name='user2')
]
HTML转义
步骤
- 创建一个htmlTest.html和配置视图
def htmlTest(request):
context = {'t1': '<h1>123</h1>'}
return render(request, 'htmlTest.html', context) - 在htmlTest.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ t1 }}
</body>
</html> - 配置url
from django.conf.urls import urlfrom booktest import views
urlpatterns = [
url(r'^$', views.index, name='index'),
# url(r'(\d+)', views.show, name='show'),
url(r'^index2/$', views.index2, name='index2'),
url(r'^user1/$', views.user1, name='user1'),
url(r'^user2/$', views.user2, name='user2'),
url(r'^htmlTest/$', views.htmlTest)
]
- 不行转义(默认是转义) safe
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ t1|safe }}
</body>
</html> - 关闭转义的另外一种方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ t1|safe }}
{% autoescape off%}
{{ t1 }}
{% endautoescape %}
</body>
</html>