創(chuàng)建一個項目bcxt_tmpl
創(chuàng)建一個應用learn
配置url
?
i.在應用下創(chuàng)建一個urls.py模塊
?
i.把我們應用創(chuàng)建的urls.py引入到項目里(做好關聯(lián))
url(r'^learn/', include(learn.urls))
視圖
?
i.首先應該先創(chuàng)建HTML頁面
默認配置下,Django 的模板系統(tǒng)會自動找到app下面的templates文件夾中的模板文件罐呼。
?
i.創(chuàng)建視圖
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 是返回模板渲染
模板
顯示一個基本的字符串在網(wǎng)頁上
步驟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 循環(huán) 和 List內(nèi)容的顯示
簡單總結一下:一般的變量之類的用 {{ }}(變量)港谊,功能類的,比如循環(huán)释液,條件判斷是用 {% %}(標簽)
?
i.在視圖函數(shù)里面定義一個列表給模板傳過去
from django.shortcuts import render
Create your views here.def home(request):
alist = ['python全棧+人工智能', '大數(shù)據(jù)技術', '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>
顯示字典中內(nèi)容
?步驟1 在views.py里面配置內(nèi)容
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:在模板里面獲取內(nèi)容并展示
?調用字典的值可以用 鍵名(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 循環(huán)的詳細操作
<!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})
網(wǎng)頁跳轉
?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')
最關鍵的一步侄泽,我們?nèi)绾卧诰W(wǎng)頁中進行跳轉,利用模板語法
<!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更改在數(shù)據(jù)庫里面顯示的字段名
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更改在數(shù)據(jù)庫里面顯示的字段名
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 %}
如果有數(shù)據(jù)走這個分支
{% empty %}
如果沒數(shù)據(jù)走這個分支
{% endfor %}
{{ forloop.counter }} # 表示循環(huán)的第幾次
反向解析
步驟
- 創(chuàng)建一個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> - 可以帶參數(shù)
<!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>
模板繼承
步驟一(模板繼承) - 創(chuàng)建一個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內(nèi)容鼓拧,剩下一個空白的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>新填充的內(nèi)容</h1>
{% endblock %}
三層繼承結構
舉個栗子(如圖)
因為之前已經(jīng)創(chuàng)建了base.html,我們現(xiàn)在只需要創(chuàng)建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模板叠赦,譬如創(chuàng)建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轉義
步驟
- 創(chuàng)建一個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>