什么是传递给django视图函数的HttpRequest元数据

时间:2013-02-12 18:27:11

标签: python django http django-views httprequest

django文档声明:

  

当请求页面时,Django会创建一个HttpRequest对象   包含有关请求的元数据。然后Django加载适当的   视图,将HttpRequest作为第一个参数传递给视图   功能。每个视图都负责返回一个HttpResponse   对象

示例:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

每个视图函数都将HttpRequest对象作为其第一个参数,通常将其命名为request。

请求参数保存什么类型的元数据并在调用时传递给view函数?

1 个答案:

答案 0 :(得分:1)

查看docs

它包含http request的各种属性的python表示。

示例 -

request.path # the url (excluding domain)
request.method # eg GET or POST
request.cookies
request.user # A django.contrib.auth.models.User object representing the currently logged-in user
request.META # A standard Python dictionary containing all available HTTP headers