python 创建一个空向量_Python之Django系列-创建第一个应用-5
上一篇:Python之Django系列-創(chuàng)建第一個(gè)應(yīng)用-4
這一章我們會(huì)講到視圖層怎么與數(shù)據(jù)庫操作并返回?cái)?shù)據(jù)到模板層進(jìn)行渲染最終顯示在頁面上
投票應(yīng)用基本上會(huì)有這么幾個(gè)視圖
- 問題列表頁
- 問題詳情頁
- 問題結(jié)果頁
- 投票處理器
在Django中,網(wǎng)頁和其他內(nèi)容都是通過視圖派生而來,而視圖可以看做Python里面的一個(gè)方法或函數(shù),現(xiàn)在開始我們創(chuàng)建以上幾個(gè)視圖
找到polls/views.py文件并進(jìn)行編輯
from django.http import HttpResponse#問題詳情頁def detail(request, question_id): return HttpResponse("當(dāng)前查看的問題 %s." % question_id)#問題結(jié)果頁def results(request, question_id): return HttpResponse("查看問題的結(jié)果 %s." % question_id)#投票處理器def vote(request, question_id): return HttpResponse("進(jìn)行投票 %s." % question_id)然后把這些視圖添加到polls/urls.py文件
urlpatterns = [ path('', views.index, name='index'),#問題列表頁 path('/', views.detail, name='detail'),#問題詳情頁 path('/results/', views.results, name='results'),#問題結(jié)果頁 path('/vote/', views.vote, name='vote'),#投票處理器]然后再啟動(dòng)你的服務(wù)
python manage.py runserver打開瀏覽器分別訪問如下地址:
http://127.0.0.1:8000/polls/1/ 瀏覽器打印:當(dāng)前查看的問題 1.
http://127.0.0.1:8000/polls/1/results/ 瀏覽器打印:查看問題的結(jié)果 1.
http://127.0.0.1:8000/polls/1/vote/ 瀏覽器打印:進(jìn)行投票 1.
注意path方法中的路徑有一個(gè)占位符,int代表請(qǐng)求參數(shù)類型,question_id映射視圖里面寫的參數(shù)question_id,而視圖中的方法request則可以理解為http的request請(qǐng)求參數(shù),后面會(huì)講到
到這,我們能簡(jiǎn)單的理解視圖的一個(gè)工作流程,但是我們是需要和數(shù)據(jù)庫交互,并把數(shù)據(jù)庫保存的數(shù)據(jù)也展示在頁面,首頁我們先在polls目錄下面創(chuàng)建一個(gè)templates目錄,接著在templates目錄里面繼續(xù)創(chuàng)建一個(gè)polls目錄,注意templates目錄必須命名為此,不然會(huì)報(bào)錯(cuò),原理是服務(wù)啟動(dòng)時(shí),會(huì)掃描mysite/settings.py文件中的TEMPLATES變量,該變量的模板引擎使用的是django.template.backends.django.DjangoTemplates,而該引擎掃描的是INSTALLED_APPS中的所有templates目錄
問題列表頁
現(xiàn)在我們開始來實(shí)現(xiàn)我們的問題列表頁,路徑應(yīng)為polls/templates/polls/index.html,我們開始html編輯前,需要對(duì)html語言有一定基礎(chǔ),如果需要,后續(xù)還會(huì)出html系列文章,如下:
問題列表頁面{% if latest_question_list %} {% for question in latest_question_list %} {{ question.question_text }} {% endfor %} {% else %}No polls are available.
{% endif %}同時(shí)改造下polls/views.py中的index方法如下:
from django.http import HttpResponsefrom django.template import loaderfrom .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))該方法中的Question.objects如果不清楚,可以查看Python之Django系列-創(chuàng)建第一個(gè)應(yīng)用-4
get_template方法是加載模板,template.render是指把context內(nèi)容渲染到模板,此時(shí)再打開頁面http://127.0.0.1:8000/polls/將會(huì)看到一個(gè)列表
當(dāng)然Django為我們提供了一個(gè)更簡(jiǎn)便的方法render,上面的index經(jīng)改造后如下:
from django.shortcuts import renderfrom .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)這樣是不是看起來更簡(jiǎn)潔
問題詳情頁
問題詳情頁視圖如下:
from django.http import Http404from django.shortcuts import renderfrom .models import Question# ...為了讓文章篇幅更短,此處省略其他方法def detail(request, question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return render(request, 'polls/detail.html', {'question': question})模板代碼如下,路徑應(yīng)為polls/templates/polls/detail.html
問題詳情頁{{ question }}然后打開瀏覽器測(cè)試 http://127.0.0.1:8000/polls/1/ 就能查詢出數(shù)據(jù)中該條數(shù)據(jù)的顯示
下一篇:Python之Django系列-創(chuàng)建第一個(gè)應(yīng)用-6
總結(jié)
以上是生活随笔為你收集整理的python 创建一个空向量_Python之Django系列-创建第一个应用-5的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ivx中字体显示_iVX云服务费用优化
- 下一篇: python脚本转换成apk_apkto