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

上一篇:Python之Django系列-创建第一个应用-4

这一章我们会讲到视图层怎么与数据库操作并返回数据到模板层进行渲染最终显示在页面上

投票应用基本上会有这么几个视图

  • 问题列表页
  • 问题详情页
  • 问题结果页
  • 投票处理器

在Django中,网页和其他内容都是通过视图派生而来,而视图可以看做Python里面的一个方法或函数,现在开始我们创建以上几个视图

找到polls/views.py文件并进行编辑

from django.http import HttpResponse#问题详情页def detail(request, question_id):    return HttpResponse("当前查看的问题 %s." % question_id)#问题结果页def results(request, question_id):    return HttpResponse("查看问题的结果 %s." % question_id)#投票处理器def vote(request, question_id):    return HttpResponse("进行投票 %s." % question_id)

然后把这些视图添加到polls/urls.py文件

urlpatterns = [    path('', views.index, name='index'),#问题列表页    path('/', views.detail, name='detail'),#问题详情页    path('/results/', views.results, name='results'),#问题结果页    path('/vote/', views.vote, name='vote'),#投票处理器]

然后再启动你的服务

python manage.py runserver

打开浏览器分别访问如下地址:

http://127.0.0.1:8000/polls/1/ 浏览器打印:当前查看的问题 1.

http://127.0.0.1:8000/polls/1/results/ 浏览器打印:查看问题的结果 1.

http://127.0.0.1:8000/polls/1/vote/ 浏览器打印:进行投票 1.

注意path方法中的路径有一个占位符,int代表请求参数类型,question_id映射视图里面写的参数question_id,而视图中的方法request则可以理解为http的request请求参数,后面会讲到

ebcad66d397f7091a1b50e6d735d44c6.png

到这,我们能简单的理解视图的一个工作流程,但是我们是需要和数据库交互,并把数据库保存的数据也展示在页面,首页我们先在polls目录下面创建一个templates目录,接着在templates目录里面继续创建一个polls目录,注意templates目录必须命名为此,不然会报错,原理是服务启动时,会扫描mysite/settings.py文件中的TEMPLATES变量,该变量的模板引擎使用的是django.template.backends.django.DjangoTemplates,而该引擎扫描的是INSTALLED_APPS中的所有templates目录

问题列表页

现在我们开始来实现我们的问题列表页,路径应为polls/templates/polls/index.html,我们开始html编辑前,需要对html语言有一定基础,如果需要,后续还会出html系列文章,如下:

    问题列表页面{% if latest_question_list %}    
{% for question in latest_question_list %} {{ question.question_text }} {% endfor %} {% else %}

No polls are available.

{% endif %}

同时改造下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系列-创建第一个应用-4

get_template方法是加载模板,template.render是指把context内容渲染到模板,此时再打开页面http://127.0.0.1:8000/polls/将会看到一个列表

当然Django为我们提供了一个更简便的方法render,上面的index经改造后如下:

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)

这样是不是看起来更简洁

问题详情页

问题详情页视图如下:

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})

模板代码如下,路径应为polls/templates/polls/detail.html

    问题详情页{{ question }}

然后打开浏览器测试 http://127.0.0.1:8000/polls/1/ 就能查询出数据中该条数据的显示

下一篇:Python之Django系列-创建第一个应用-6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值