加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Django 14天从小白到进阶- Day3 搞定Views组件

发布时间:2020-05-25 03:43:47 所属栏目:Python 来源:互联网
导读:本节内容 路由系统 models模型 admin views视图 template模板 我们已经学过了基本的view写法 单纯返回字符串 返回html文件 加装饰器 只允许特定方法 HttpRequ

本节内容

  • 路由系统
  • models模型
  • admin
  • views视图
  • template模板

我们已经学过了基本的view写法

单纯返回字符串

Page not found')
# Return a "created" (201) response code.
#return HttpResponse(status=201)

返回html文件  

加装饰器

users = models.Account.objects.all() return render(request,'account.html',{'account_list':users}) </pre>

只允许特定方法

@require_http_methods(["GET","POST"])
def my_view(request):

I can assume now that only GET or POST requests make it this far

# ...
pass

  

  

HttpRequest 对象

我们知道每个视图函数的request参数这个东西,但一直没正式讲,它到底是个什么样的存在?  

每一个用户请求在到达视图函数的同时,django会自动创建一个HttpRequest对象并把这个对象当做第一个参数传给要调用的views方法,HttpRequest对象里封装了本次请求所涉及的用户浏览器端数据、服务器端数据等,在views里可以通过request对象来调取相应的属性。

A string representing the scheme of the request (orusually).

HttpRequest.path A string representing the full path to the requested page,not including the scheme or domain.

Example: "/music/bands/the_beatles/"

HttpRequest.method A string representing the HTTP method used in the request

 

HttpRequest.content_type A string representing the MIME type of the request,parsed from the CONTENT_TYPE header.

是描述消息内容类型的因特网标准。

MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。

HttpRequest.GET A dictionary-like object containing all given HTTP GET parameters. See the QueryDict documentation below.

HttpRequest.POSTHttpRequest.COOKIES A dictionary containing all cookies. Keys and values are strings.

HttpRequest.FILES A dictionary-like object containing all uploaded files. Each key in FILES is the name from the . Each value in FILES is an UploadedFile.

FILES will only contain data if the request method was POST and the

that posted to the request had enctype="multipart/form-data". Otherwise,FILES will be a blank dictionary-like object.

A dictionary containing all available HTTP headers. Available headers depend on the client and server,but here are some examples:

    – The length of the request body (as a string).
  • – The MIME type of the request body.
  • – Acceptable content types for the response.
  • – Acceptable encodings for the response.
  • – Acceptable languages for the response.
  • – The HTTP Host header sent by the client.
  • – The referring page,if any.
  • – The client’s user-agent string.
  • – The query string,as a single (unparsed) string.
  • – The IP address of the client.
  • – The hostname of the client.
  • – The user authenticated by the Web server,if any.
  • – A string such asor.
  • – The hostname of the server.
  • – The port of the server (as a string).

Attributes set by middleware

Some of the middleware included in Django’s contrib apps set attributes on the request. If you don’t see the attribute on a request,be sure the appropriate middleware class is listed in.

From the: A readable and writable,dictionary-like object that represents the current session.

HttpRequest.user   From the AuthenticationMiddleware: An instance of AUTH_USER_MODEL representing the currently logged-in user. If the user isn’t currently logged in,user will be set to an instance of AnonymousUser. You can tell them apart with is_authenticated,like so:

Methods

除了属性,HttpRequest对象还带有很多方法

()

返回网站服务器地址,Example:

()

返回服务器主机端口

HttpRequest.get_full_path() Returns the path,plus an appended query string,if applicable.

Example: "/music/bands/the_beatles/?print=true"

HttpRequest.build_absolute_uri(location) Returns the absolute URI form of location. If no location is provided,the location will be set to request.get_full_path().

If the location is already an absolute URI,it will not be altered. Otherwise the absolute URI is built using the server variables available in this request.

Example: "https://example.com/music/bands/the_beatles/?print=true"

HttpRequest.is_secure() Returns True if the request is secure; that is,if it was made with HTTPS.

HttpRequest.is_ajax()判断是否ajax请求

objects

In contrast toobjects,which are created automatically by Django,objects are your responsibility. Each view you write is responsible for instantiating,populating,and returning an.

Theclass lives in themodule.

Usage

Passing strings

Typical usage is to pass the contents of the page,as a string,to theconstructor:

>> >> >>

But if you want to add content incrementally,you can useas a file-like object:

>> >> Here's the text of the Web page.

">> Here's another paragraph.

"

Telling the browser to treat the response as a file attachment

(编辑:安卓应用网)

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

    推荐文章
      热点阅读