每個(gè)視圖必須要做的只有兩件事:返回一個(gè)包含被請(qǐng)求頁(yè)面內(nèi)容的 HttpResponse 對(duì)象,或者拋出一個(gè)異常,比如 Http404 。
當(dāng)然你也可以從數(shù)據(jù)庫(kù)里讀取記錄,可以使用一個(gè)模板引擎(比如 Django 自帶的,或者其他第三方的),可以生成一個(gè) PDF 文件,或者輸出一個(gè) XML
寫(xiě)一個(gè)真正有用的視圖
我們?cè)?index() 函數(shù)里插入了一些新內(nèi)容,在上一節(jié)中講過(guò)如果創(chuàng)建視圖,讓它能展示數(shù)據(jù)
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
這里有個(gè)問(wèn)題:頁(yè)面的設(shè)計(jì)寫(xiě)死在視圖函數(shù)的代碼里的。如果你想改變頁(yè)面的樣子,你需要編輯 Python 代碼。所以讓我們使用 Django 的模板系統(tǒng)
首先,在你的 polls 目錄里創(chuàng)建一個(gè) templates 目錄。Django 將會(huì)在這個(gè)目錄里查找模板文件。
你項(xiàng)目中setting.py的 TEMPLATES 配置項(xiàng)描述了 Django 如何載入和渲染模板。默認(rèn)的設(shè)置文件設(shè)置了 DjangoTemplates 后端,并將 APP_DIRS 設(shè)置成了 True。這一選項(xiàng)將會(huì)讓 DjangoTemplates 在每個(gè) INSTALLED_APPS 文件夾中尋找 "templates" 子目錄。所以你只需要使用 polls/index.html 就可以引用到這一模板了。
將下面的代碼輸入到剛剛創(chuàng)建的polls/index.html 模板文件中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django官方網(wǎng)站 </title>
{% load staticfiles %}
</head>
<body>
<p> {{ hello }}</p>
<img src="{% static "img/2019-04-25.png" %}" alt="My image">
</body>
</html>
然后,讓我們更新一下 polls/views.py 里的 index 視圖來(lái)使用模板:
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
context = 'hello word'
template = loader.get_template('polls/index.html')
return HttpResponse(template.render(context, request))
上述代碼的作用是,載入 polls/index.html 模板文件,并且向它傳遞一個(gè)上下文(context)。這個(gè)上下文是一個(gè)字典,它將模板內(nèi)的變量映射為 Python 對(duì)象。
一個(gè)快捷函數(shù): render()
「載入模板,填充上下文,再返回由它生成的 HttpResponse 對(duì)象」是一個(gè)非常常用的操作流程。于是 Django 提供了一個(gè)快捷函數(shù),我們用它來(lái)重寫(xiě) index() 視圖:
from django.shortcuts import render
from .models import Question
def index(request):
context = 'hello word'
return render(request, 'polls/index.html', context)
我們不再需要導(dǎo)入 loader 和 HttpResponse 。不過(guò)如果你還有其他函數(shù)(比如說(shuō) detail, results, 和 vote )需要用到它的話,就需要保持 HttpResponse 的導(dǎo)入。
總結(jié):
Django內(nèi)views常用到到有三個(gè)方法,分別是:
1.HttpResponse() :它是作用是內(nèi)部傳入一個(gè)字符串或變量,然后發(fā)給瀏覽器。
2.render():它的作用就是將數(shù)據(jù)填充進(jìn)模板文件,最后把結(jié)果返回給瀏覽器。可接收三個(gè)參數(shù),一是request參數(shù),二是待渲染的html模板文件,三是保存具體數(shù)據(jù)的字典參數(shù)。
3.redirect()接受一個(gè)URL參數(shù),表示讓瀏覽器跳轉(zhuǎn)去指定的URL.
舉例:
def index(request):
# 業(yè)務(wù)邏輯代碼
return HttpResponse("OK")
def index(request):
# 業(yè)務(wù)邏輯代碼
return render(request, "index.html", {"name": "monicx", "hobby": ["reading", "blog"]})
def index(request):
# 業(yè)務(wù)邏輯代碼
return redirect("https://blog.csdn.net/miaoqinian")
## 拋出 404 錯(cuò)誤?
如果指定問(wèn)題 ID 所對(duì)應(yīng)的問(wèn)題不存在,這個(gè)視圖就會(huì)拋出一個(gè) Http404 異常。
我們暫時(shí)在 polls/detail.html 里把下面這段輸進(jìn)去:
{{ question }}
一個(gè)快捷函數(shù): get_object_or_404()?
嘗試用 get() 函數(shù)獲取一個(gè)對(duì)象,如果不存在就拋出 Http404 錯(cuò)誤也是一個(gè)普遍的流程。Django 也提供了一個(gè)快捷函數(shù),下面是修改后的詳情 detail() 視圖代碼:
from django.shortcuts import get_object_or_404, render
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})
也有 get_list_or_404() 函數(shù),工作原理和 get_object_or_404() 一樣,除了 get() 函數(shù)被換成了 filter() 函數(shù)。如果列表為空的話會(huì)拋出 Http404 異常。
更多內(nèi)容參考:編寫(xiě)你的第一個(gè) Django 應(yīng)用,第 3 部分