繼上篇django2.0入門教程第二節(jié),介紹了對django2.0模型類models的操作,本篇主要講視圖views和模板template
django的視圖用于處理url請求,并將響應的數(shù)據(jù)傳遞到模板,最終瀏覽器將模板數(shù)據(jù)進行渲染顯示,用戶就得到了想要的結果
作為一個簡易的投票系統(tǒng), 除了index(主頁), 還需要detail(詳情頁), results(結果頁), vote(投票頁) 這些視圖。
增加視圖:polls/views.py
#_*_coding:utf8_*_
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse("你好,歡迎來到投票系統(tǒng)的主頁")
def detail(request, question_id):
return HttpResponse('你正在查看問題%s' % question_id)
def results(request, question_id):
response = '你正在查看問題%s的結果'
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse('你正在給問題%s投票' % question_id)
配置url:polls/urls.py
#_*_coding:utf8_*_
from django.urls import path
from . import views
urlpatterns = [
# /polls/
path('', views.index, name='index'),
# /polls/1/
path('<int:question_id>/', views.detail, name='detail'),
# /polls/1/results/
path('<int:question_id>/results/', views.results, name='results'),
# /polls/1/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
url訪問:
你好,歡迎來到投票系統(tǒng)的主頁
http://127.0.0.1:8000/polls/1/
你正在查看問題1
http://127.0.0.1:8000/polls/1/results/
你正在查看問題1的結果
http://127.0.0.1:8000/polls/1/vote/
你正在給問題1投票
這樣的視圖內容過于簡略粗糙, 在后臺添加多一些數(shù)據(jù),供后續(xù)調用:

通過視圖直接返回的數(shù)據(jù),顯示格式很單一,要想顯示豐富的數(shù)據(jù)形式,就需要引用模板,用獨立的模板文件來呈現(xiàn)內容。
新增模板:polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{question.id}}/">{{question.question_text}}</a></li>
{% endfor %}
</ul>
{% else %}
<p>問題為空</p>
{% endif %}
修改視圖: polls/views.py 傳遞變量給模板
#_*_coding:utf8_*_
from django.shortcuts import HttpResponse
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {'latest_question_list': latest_question_list}
return HttpResponse(template.render(context, request))
訪問:http://127.0.0.1:8000/polls/

使用便捷寫法--render()函數(shù):
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
開發(fā)中,直接使用render()即可,盡可能精簡代碼
詳情頁的展示:
polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404('問題不存在')
return render(request, 'polls/detail.html', {'question': question})
新增詳情頁:polls/templates/polls/detail.html
{{ question }}
http://127.0.0.1:8000/polls/3/
問題3
訪問不存在的問題id時,會報404
http://127.0.0.1:8000/polls/4/

404頁面拋出的便捷寫法:get_object_or_404()
polls/views.py
from django.shortcuts import render, get_object_or_404
from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
詳情頁輸出關聯(lián)數(shù)據(jù)表:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
將問題下有關聯(lián)選項一并輸出:

去掉url的硬編碼格式
原本index.html的url定義形式是這樣的:
<li><a href="/polls/{{question.id}}/">{{question.question_text}}</a></li>
這種寫法屬于硬編碼方式,并不好,因為一旦詳情頁的url改變,就要去改變相應的html文件,如果html文件有很多處都引用了,逐一排查需要耗費相當多時間。將url改成以下形式即可實現(xiàn)自動轉換:
<li><a href="{% url 'detail' question.id %}">{{question.question_text}}</a></li>
修改url配置:
將polls/urls.py的詳情頁url由:path('<int:question_id>/', views.detail, name='detail')改為:path('specifics/<int:question_id>/', views.detail, name='detail')
此時,index.html的url會自動由 http://127.0.0.1:8000/polls/1/ 轉為 http://127.0.0.1:8000/polls/specifics/1/
url的命名空間
現(xiàn)實項目中,一個django項目是會有多個應用的,為了將這些應用進行區(qū)分,需要使用命名空間
#_*_coding:utf8_*_
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
將index.html的url生成代碼加上命名空間:
<li><a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
源碼下載
如果對django2.0教程感興趣,請關注我的簡書,持續(xù)更新中...