日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 创建一个空向量_Python之Django系列-创建第一个应用-5

發(fā)布時(shí)間:2024/7/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 创建一个空向量_Python之Django系列-创建第一个应用-5 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一篇: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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。