每一個視圖必需要作的只有兩件事:html
返回一個包含被請求頁面內容的HttpResponse對象,或拋出一個異常,好比Http404。python
至於你還想幹些什麼,隨便你。數據庫
你的視圖能夠從數據庫裏讀取記錄,可使用一個模板引擎(好比django自帶的,或者其餘第三方的),能夠生成一個PDF文件,能夠輸出一個XML,建立一個ZIP文件,你能夠作任何你想作的事,使用任何你想用的python庫。django
django只要求返回的是一個HttpResponse,或者拋出一個異常後端
由於django自帶的數據庫API很方便,咱們在前面學過,因此咱們試試在視圖裏使用它。瀏覽器
在index()函數裏插入一些新的內容,讓它能展現數據庫裏以發佈日期排序的最近5個投票問題,以空格分割:session
from django.shortcuts import render # Create your views here. 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)
其餘視圖函數不變(detail,results,vote)app
爲了演示,咱們添加6個問題:函數
查看數據庫中的數據spa
瀏覽器訪問http://127.0.0.1:8000/polls/,觸發index視圖函數:
這裏有個問題:頁面的設計寫死在視圖函數的代碼裏的。若是你想改變頁面的樣子,你須要編輯python代嗎。因此讓咱們使用django的模板系統,只要建立一個視圖,就能夠將頁面的設計從代碼中分離出來。
使用django的模板系統
首先,在你的polls項目裏建立一個templates目錄。django將會在這個目錄裏查找模板文件。
你的項目的TEMPLATES配置項描述了django如何載入和渲染模板。默認的設置文件設置了DjangoTemplates後端,並將APP_DIRS設置成了True。這一選項將會讓DjangoTemplates在每一個INSTALLED_APPS文件夾中尋找」templates」子目錄。
settings.py:
INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
在你剛剛建立的templates目錄裏,再建立一個目錄polls,而後在其中新建一個文件index.html。換句話說,你的模板文件的路徑應該是polls/templates/polls/index.html。由於django會尋找到對應的app_directories(不太理解),因此你只須要使用polls/index.html就能夠引用到這一模板了。
模板命名空間
雖然咱們如今能夠將模板文件直接在polls/templates文件中(而不是再創建一個polls子文件夾),可是這樣作不太好。django將會選擇第一個匹配的模板文件,若是你有一個模板文件正好和另外一個應用中的某個模板文件重名,django沒有辦法區分它們。咱們須要幫助django選擇正確的模板,最簡單的方法就是把它們放入個字的命名空間中,也就是把這些模板放入一個和自身應用重名的子文件夾裏。
因此咱們在templates文件夾下,新建一個跟應用重名的文件夾」polls」,而後在該子文件夾下新建一個index.html文件:polls/templates/polls/index.hmtl
在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>No polls are available.</p> {% endif %}
而後,讓咱們更新一下polls/views.py裏的index視圖來使用模板:
視圖函數中使用模板:
polls/view.py:
from django.http 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))
上下文:
上述代碼的做用是,載入polls/index.html模板文件,而且向它傳遞一個上下文(context)。這個上下文是一個字典,它將模板內的變量映射爲python對象。
此處context是一個字典,渲染模板時經過template.render()方法把context和請求對象傳入到模板文件中,在模板文件中,對context中的對象進行渲染
再次觸發視圖函數,渲染模板
瀏覽器訪問」polls/」,將會看到一個無序列表,列出了咱們在以前添加的投票問題,連接指向了這個投票的詳情頁。
點擊連接: