Django 14天从小白到进阶- Day3 搞定Views组件
|
本节内容
我们已经学过了基本的view写法 单纯返回字符串 Page not found')
返回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
HttpRequest 对象我们知道每个视图函数的request参数这个东西,但一直没正式讲,它到底是个什么样的存在? 每一个用户请求在到达视图函数的同时,django会自动创建一个HttpRequest对象并把这个对象当做第一个参数传给要调用的views方法,HttpRequest对象里封装了本次请求所涉及的用户浏览器端数据、服务器端数据等,在views里可以通过request对象来调取相应的属性。 A string representing the scheme of the request ( 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.
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 |
