在Django中,網頁和其他內容是通過視圖傳遞的。每個視圖由一個簡單的Python函數表示,Django將通過檢查請求的URL(准確地說,是域名后面的部分URL)來選擇一個視圖。
例如,用戶在瀏覽器中訪問 <<domain>>/newsarchive/<year>/<month>/ diango的URLConfs 將請求URL與對應的views function 匹配,調用view function 進行數據處理,然后選擇對應的template模板進行渲染展示或直接數據返回。

在我們的poll app中,我們將會創建以下四個視圖views:
- Question “index” page – displays the latest few questions.
- Question “detail” page – displays a question text, with no results but with a form to vote.
- Question “results” page – displays results for a particular question.
- Vote action – handles voting for a particular choice in a particular question.
編寫views
polls/views.py:
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
綁定URL與Views
polls/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
URLconfs 中,正則表達式中的分組()作為參數傳遞給view,如url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail')
假如請求url為 polls/34/ 相當於調用detail(request,question_id='34')
分別訪問一下url可見調用不同的view 函數進行相應
http://localhost:8000/polls/
http://localhost:8000/polls/34/
http://localhost:8000/polls/34/results/
http://localhost:8000/polls/34/vote/
編寫Views的數據庫處理邏輯
view的主要工作:獲取請求內容,調用數據庫model獲取數據庫數據,調用業務處理Model邏輯處理,將處理結果渲染到指定的模板template中,響應response到客戶端瀏覽器
創建 polls/templates,django會在在app目錄下查找templates目錄作為模板路徑
創建 polls/templates/polls/index.html
Because of how the app_directories template loader works as described above, you can refer to this template within Django simply as polls/index.html.
(當然templates下不創建polls,模板路徑調用index.html 也可以,但是強烈不建議這樣,因為避免出現不同的app中有相同名稱的模板文件時讀取區分不出來)
{% 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 %}
更新 index view in polls/views.py:
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)
訪問 http://localhost:8000/polls/

編寫Views 404異常
更新detail view in polls/views.py:
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
get_object_or_404(),get_list_or_404() 當獲取不到對象時,返回404頁面
訪問 http://localhost:8000/polls/34/

使用template模板
添加 polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
django模板系統使用雙大括號訪問變量屬性,如{{question.question_text}},
django模板中,使用 {% %} 將原生pyton語句包含起來,其中以上實例中,使用了for循環:{% for %} {% endfor %}
訪問 http://localhost:8000/polls/1

修改template模板中的hardcoded URLs,統一使用{% url %} 標簽替換
如polls/index.html 中的 <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
修改為:<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

這種URL 查找方式是通過polls.urls 中的name來匹配,如:
mysite/urls.py:
url(r'^polls/', include('polls.urls', namespace="polls")),
polls/urls.py:
# the 'name' value as called by the {% url %} template tag
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
添加 URL namespace
如上例,使用{% url 'detail' %} 可以根據polls.urls 中的name='detail' 來匹配。如果在同一個project下有多個app,其中都有name='detail' 時,又該如何匹配views呢?
解決方法是,添加namespace到URLconf中,如在polls/urls.py 中添加: app_name = 'polls'

則可以在模板中修改{% url 'detail' %} 為 {% url 'polls:detail' %}

訪問:http://localhost:8000/polls/
點擊“what's up” 鏈接

