厦门方易网站制作有限公司,网络对企业管理的影响,百度seo优化价格,展示类网站开发费用上一篇#xff1a;Python之Django系列-创建第一个应用-4这一章我们会讲到视图层怎么与数据库操作并返回数据到模板层进行渲染最终显示在页面上投票应用基本上会有这么几个视图问题列表页问题详情页问题结果页投票处理器在Django中#xff0c;网页和其他内容都是通过视图派生而…上一篇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, nameindex),#问题列表页 path(/, views.detail, namedetail),#问题详情页 path(/results/, views.results, nameresults),#问题结果页 path(/vote/, views.vote, namevote),#投票处理器]然后再启动你的服务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请求参数后面会讲到到这我们能简单的理解视图的一个工作流程但是我们是需要和数据库交互并把数据库保存的数据也展示在页面首页我们先在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系列-创建第一个应用-4get_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(pkquestion_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