Django 请求和响应周期——HttpRequest 和 HttpResponse 对象
先决条件 - Django 中的视图 | Python
在我们开始使用视图附带的便捷方法之前,让我们先谈谈请求和响应周期。因此,当请求发生在 Django 服务器上时,会发生一些事情。其中之一是中间件。
中间件
中间件就像是请求和响应之间的中间地带。它就像一个窗口,数据通过它。就像在窗户里一样,光线进出房子。类似地,当发出请求时,它会通过中间件移动到视图,并且数据作为响应通过中间件传递。
这是 Django 中安装的默认中间件。
您可以添加中间件。我们将在下一篇文章中讨论这个问题。
请求和响应对象:
Django 使用请求和响应对象通过系统传递状态。
当一个页面被请求时,Django 会创建一个 HttpRequest 对象,其中包含有关请求的元数据。然后 Django 加载适当的视图,将 HttpRequest 作为第一个参数传递给视图函数。每个视图负责返回一个 HttpResponse 对象。
HttpRequest 和 HttpResponse 对象示例
- 为了解释这些对象,让我们在views.py中创建一个视图主页,如下所示
Python3
# importing HttResponse from library
from django.http import HttpResponse
def home(request):
# request is handled using HttpResponse object
return HttpResponse("Any kind of HTML Here")
Python3
# importing view from views.py
from .views import home
urlpatterns = [
path('', home),
]
- 为了处理请求,让我们在urls.py中将一个 URL 映射到这个视图
Python3
# importing view from views.py
from .views import home
urlpatterns = [
path('', home),
]
- 现在可以运行服务器在浏览器中看到以下内容
HttpRequest 属性 – Django
您可以将以下属性与 HttpRequest 一起使用以进行高级操作Attribute Description HttpRequest.scheme A string representing the scheme of the request (HTTP or HTTPS usually). HttpRequest.body It returns the raw HTTP request body as a byte string. HttpRequest.path It returns the full path to the requested page does not include the scheme or domain. HttpRequest.path_info It shows path info portion of the path. HttpRequest.method It shows the HTTP method used in the request. HttpRequest.encoding It shows the current encoding used to decode form submission data. HttpRequest.content_type It shows the MIME type of the request, parsed from the CONTENT_TYPE header. HttpRequest.content_params It returns a dictionary of key/value parameters included in the CONTENT_TYPE header. HttpRequest.GET It returns a dictionary-like object containing all given HTTP GET parameters. HttpRequest.POST It is a dictionary-like object containing all given HTTP POST parameters. HttpRequest.COOKIES It returns all cookies available. HttpRequest.FILES It contains all uploaded files. HttpRequest.META It shows all available Http headers. HttpRequest.resolver_match It contains an instance of ResolverMatch representing the resolved URL.
HttpRequest 方法 – Django
您可以将以下方法与 HttpRequest 一起使用以进行高级操作Attribute Description HttpRequest.get_host() It returns the original host of the request. HttpRequest.get_port() It returns the originating port of the request. HttpRequest.get_full_path() It returns the path, plus an appended query string, if applicable. HttpRequest.build_absolute_uri (location) It returns the absolute URI form of location. HttpRequest.get_signed_cookie (key, default=RAISE_ERROR, salt=”, max_age=None) It returns a cookie value for a signed cookie, or raises a django.core.signing.BadSignature exception if the signature is no longer valid. HttpRequest.is_secure() It returns True if the request is secure; that is, if it was made with HTTPS. HttpRequest.is_ajax() It returns True if the request was made via an XMLHttpRequest.
HttpResponse 属性 – Django
您可以将以下属性与 HttpResponse 一起使用以进行高级操作Attribute Description HttpResponse.content A bytestring representing the content, encoded from a string if necessary. HttpResponse.charset It is a string denoting the charset in which the response will be encoded. HttpResponse.status_code It is an HTTP status code for the response. HttpResponse.reason_phrase The HTTP reason phrase for the response. HttpResponse.streaming It is false by default. HttpResponse.closed It is True if the response has been closed.
HttpResponse 方法 – Django
您可以将以下方法与 HttpResponse 一起使用以进行高级操作Method Description HttpResponse.__init__(content=”, content_type=None, status=200, reason=None, charset=None) It is used to instantiate an HttpResponse object with the given page content and content type. HttpResponse.__setitem__(header, value) It is used to set the given header name to the given value. HttpResponse.__delitem__(header) It deletes the header with the given name. HttpResponse.__getitem__(header) It returns the value for the given header name. HttpResponse.has_header(header) It returns either True or False based on a case-insensitive check for a header with the provided name. HttpResponse.setdefault(header, value) It is used to set default header. HttpResponse.write(content) It is used to create response object of file-like object. HttpResponse.flush() It is used to flush the response object. HttpResponse.tell() This method makes an HttpResponse instance a file-like object. HttpResponse.getvalue() It is used to get the value of HttpResponse.content. HttpResponse.readable() This method is used to create stream-like object of HttpResponse class. HttpResponse.seekable() It is used to make response object seekable.