加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django之views视图函数

发布时间:2020-12-15 17:18:36 所属栏目:大数据 来源:网络整理
导读:div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=9902600"gt; p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;"views视图函
</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">method</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">请求中使用的HTTP方法</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">GET</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">包含所有HTTP GET参数的类字典对象</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">POST</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">包含所有HTTP POST参数的类字典对象</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">REQUEST</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">该属性是POST和GET属性的集合体,先查找POST属性,然后再查找GET属性</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">COOKIES</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">包含所有cookies的标准Python字典对象</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">FILES</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">包含所有上传文件的类字典对象。FILES中的每个Key都是标签中name属性的值. FILES中的每个value 同时也是一个标准Python字典对象,包含下面三个Keys: filename: 上传文件名,用Python字符串表示 content-type: 上传文件的Content type content: 上传文件的原始内容</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">META</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">包含所有可用HTTP头部信息的字典</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">user</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">是一个django.contrib.auth.models.User 对象,代表当前登录的用户。如果访问用户当前没有登录,user将被初始化为django.contrib.auth.models.AnonymousUser的实例</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">session</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">唯一可读写的属性,代表当前会话的字典对象。只有激活Django中的session支持时该属性才可用</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">raw_post_data</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">原始HTTP POST数据,未解析过</td>

</tr>

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=9902600"&gt;
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">views视图函数属于MTV中逻辑处理的部分视图函数包含着两个对象,HttpRequest对象和HttpResponse对象


<h1 id="-httprequest-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">一.HttpRequest对象
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">HttpRequest对象在Django中会默认传到views函数中作为第一个参数


<h3 id="httprequest-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">HttpRequest的属性:
<table style="margin: 1.2em 0px; padding: 0px; border-collapse: collapse; border-spacing: 0px; font: inherit; border: 0px;">

(key)</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">has_key()</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">检查request.GET or request.POST中是否包含参数指定的Key</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">get_full_path()</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">返回包含查询字符串的请求路径,包含get方式发送的数据</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">is_secure()</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">如果请求是安全的,返回True</td>

</tr>

 django.shortcuts  HttpResponse

<span class="hljs-function" style="color: #7aa6da;"><span class="hljs-keyword" style="color: #c397d8;">def <span class="hljs-title" style="color: #969896;">login<span class="hljs-params" style="color: #e78c45;">(req):
<span class="hljs-keyword" style="color: #c397d8;">return HttpResponse(<span class="hljs-string" style="color: #b9ca4a;">"welcome")


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">在HttpResponse的基础上拓展了两个常用方法render方法和redirect方法


<h3 id="render-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">render方法
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">render方法用于将有关信息利用模板添加到要给用户发送的html文档中render方法的参数:


<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">request:即视图函数中的HttpRequest对象,也就是第一个参数
<li style="margin: 0.5em 0px; font-size: 16px;">template_name:templates 中定义的文件,也就是html文档
<li style="margin: 0.5em 0px; font-size: 16px;">context: 要传入html文档中用于渲染呈现的数据,默认是字典格式。也可以使用locals(),表示将函数中所有的变量传给模板
<li style="margin: 0.5em 0px; font-size: 16px;">content_type: 生成的文档要使用的MIME 类型。默认为DEFAULT_CONTENT_TYPE 设置的值
<li style="margin: 0.5em 0px; font-size: 16px;">status: http的响应代码,默认是200
<li style="margin: 0.5em 0px; font-size: 16px;">using: 用于加载模板使用的模板引擎的名称

 :
     render(req,,{:})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读