当前位置: 首页 > news >正文

网站建设运维情况2019做网站需要营业执照吗

网站建设运维情况,2019做网站需要营业执照吗,沃尔玛超市网上购物app下载,phpstudy如何建设网站Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」 在我们的投票应用中#xff0c;我们需要下列几个视图#xff1a; 问题索引页——展示最近的几个投票问题。 问题详情页——展示某个投票的问题和不带结果的选项列表。 问题结果页——展示某个投票的结果。 投…Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」 在我们的投票应用中我们需要下列几个视图 问题索引页——展示最近的几个投票问题。 问题详情页——展示某个投票的问题和不带结果的选项列表。 问题结果页——展示某个投票的结果。 投票处理器——用于响应用户为某个问题的特定选项投票的操作。 在 Django 中网页和其他内容都是从视图派生而来。每一个视图表现为一个 Python 函数或者说方法如果是在基于类的视图里的话。Django 将会根据用户请求的 URL 来选择使用哪个视图 添加视图 polls/view.py def detail(request, question_id):return HttpResponse(Youre looking at question %s. % question_id)def results(request, question_id):response Youre looking at the results of question %s.return HttpResponse(response % question_id)def vote(request, question_id):return HttpResponse(Youre voting on question %s. % question_id)这段代码是一个基本的 Django 视图函数它接受一个 HTTP 请求对象 request 和一个 question_id 参数然后返回一个 HTTP 响应。 HttpResponse 是 Django 提供的一个用于构建 HTTP 响应的类。response 是一个字符串其中包含了一个占位符 %s用于展示 question_id 的值。通过 % 运算符将 question_id 的值插入到 response 字符串中形成最终的响应内容。最后用 return 语句返回该响应对象。 添加url polls/urls.py from django.urls import path from . import views urlpatterns[path(,views.index,nameindex),path(int:question_id/,views.detail,namedetail),path(int:question_id/results,views.detail,namedetail),path(int:question_id/vote,views.detail,namedetail), ]运行并访问 每个视图必须要做的只有两件事返回一个包含被请求页面内容的 HttpResponse 对象或者抛出一个异常比如 Http404 。至于你还想干些什么随便你。 你的视图可以从数据库里读取记录可以使用一个模板引擎比如 Django 自带的或者其他第三方的可以生成一个 PDF 文件可以输出一个 XML创建一个 ZIP 文件你可以做任何你想做的事使用任何你想用的 Python 库。 Django 只要求返回的是一个 HttpResponse 或者抛出一个异常。 获取最近5个投票问题 修改视图,在index() 函数里插入了一些新内容让它能展示数据库里以发布日期排序的最近 5 个投票问题以空格分割 polls/view.py from django.http import HttpResponsefrom .models import Questiondef index(request):latest_question_list Question.objects.order_by(-pub_date)[:5]output , .join([q.question_text for q in latest_question_list])return HttpResponse(output)# Leave the rest of the views (detail, results, vote) unchanged使用模板 创建文件polls/templates/polls/index.html {% if latest_question_list %}ul{% for question in latest_question_list %}lia href/polls/{{ question.id }}/{{ question.question_text }}/a/li{% endfor %}/ul {% else %}pNo polls are available./p {% endif %}修改polls/view.py中index函数 from django.http import HttpResponse from django.template import loaderfrom .models import Questiondef 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))载入 polls/index.html 模板文件并且向它传递一个上下文(context)。这个上下文是一个字典它将模板内的变量映射为 Python 对象 也可以直接使用render方法该方法将模板渲染。 from django.shortcuts import renderfrom .models import Questiondef 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)latest_question_list 为对象列表传给模板后模板取出对象中的信息 查看效果 点击名称进入详情 抛出404错误 当访问到不存在的id时可以抛出404错误 polls/view.py from django.http import Http404 from 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 }}快捷函数get_object_or_404 尝试用 get() 函数获取一个对象如果不存在就抛出 Http404 错误也是一个普遍的流程 from django.shortcuts import get_object_or_404, renderfrom .models import Question# ... def detail(request, question_id):question get_object_or_404(Question, pkquestion_id)return render(request, polls/detail.html, {question: question})表单处理 poll/templates/polls/detailhtml form action{% url polls:vote question.id %} methodpost {% csrf_token %} fieldsetlegendh1{{ question.question_text }}/h1/legend{% if error_message %}pstrong{{ error_message }}/strong/p{% endif %}{% for choice in question.choice_set.all %}input typeradio namechoice idchoice{{ forloop.counter }} value{{ choice.id }}label forchoice{{ forloop.counter }}{{ choice.choice_text }}/labelbr{% endfor %} /fieldset input typesubmit valueVote /form这个模板页面包含表单form 添加url polls/urls.py path(int:question_id/vote/, views.vote, namevote),polls/view.py from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reversefrom .models import Choice, Question# ... def vote(request, question_id):question get_object_or_404(Question, pkquestion_id)try:selected_choice question.choice_set.get(pkrequest.POST[choice])except (KeyError, Choice.DoesNotExist):# Redisplay the question voting form.return render(request,polls/detail.html,{question: question,error_message: You didnt select a choice.,},)else:selected_choice.votes 1selected_choice.save()# Always return an HttpResponseRedirect after successfully dealing# with POST data. This prevents data from being posted twice if a# user hits the Back button.return HttpResponseRedirect(reverse(polls:results, args(question.id,)))from django.shortcuts import get_object_or_404, renderdef results(request, question_id):question get_object_or_404(Question, pkquestion_id)return render(request, polls/results.html, {question: question})如果id不存在返回404如果存在则对应问题选票1投票后跳转result页面 polls/templates/polls/results.html h1{{ question.question_text }}/h1ul {% for choice in question.choice_set.all %}li{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}/li {% endfor %} /ula href{% url polls:detail question.id %}Vote again?/a选中选项点击vote进入results页面。 代码https://github.com/2504973175/mysite_django/tree/main
http://www.hkea.cn/news/14266902/

相关文章:

  • 怎么在自己做的网站上发视频教程做家装网站源码
  • 上海网站建设口碑好大同网络公司
  • 淮安新港建设有限公司网站wordpress主题公园
  • 阿里云服务器 放多个网站装饰工程施工流程步骤
  • 电子商务网站建设实训报告文章孩子学编程一年要多少钱
  • 西乡专业做网站公司给客户做一个网站ppt怎么做
  • redis做网站保定网站建设浩森宇特
  • 那个网站的域名便宜营销自己的网站
  • 石家庄建设一个网站多少钱网页设计心得体会100
  • 网站的基本知识怎样做一个公司网站
  • 沈阳点金网站建设网站后台用什么
  • 网站优化哪里可以做爱有声小说网站捡个校花做老婆
  • 利用wordpress做api提供者电商网站产品设计优化技术主要是
  • 网站开发外包维护合同范本营销策划书怎么写格式
  • 网站开发页面静态化技术谷歌seo外贸推广
  • 如何做视频教程网站惠州seo外包平台
  • 镇江网站建设优化案例分析杭州设计公司装修
  • 在服务器上部署网站企业信息
  • 苏州建设网站平台WordPress海报封面主题
  • 网站建设 站内页面连接如何搭建一个自己的服务器
  • 阿里云网站地图是怎么做的wordpress友链插件
  • 百度投诉电话24小时巩义自助建站优化
  • 未成年人做网站多少钱能注册500万公司
  • 温州网站建设价格技术山西太原网络推广
  • 馆陶网站建设公司体育用品东莞网站建设
  • 做毕业设计免费网站建设用什么程序做资讯类网站
  • 百度数据网站贵阳市白云区官方网站
  • 网站服务器租用怎样收费西安wordpress
  • 河北省电力建设第一工程公司网站做网站需要学多久
  • 建站推广哪里有建站新闻资讯域名备案的网站建设方案书模板