Django HttpResponse datatype
时间: 2023-12-05 16:03:19 浏览: 84
The `HttpResponse` class in Django is used to create an HTTP response object, which can be returned from a view function. The response can contain data in different formats, including HTML, plain text, JSON, XML, or binary data.
The `HttpResponse` object takes one mandatory argument, which is the content to be sent in the response. This content can be a string, bytes, or a file-like object.
For example, to create an HTTP response object with plain text content, you can use the following code:
```python
from django.http import HttpResponse
def my_view(request):
response = HttpResponse('Hello, world!', content_type='text/plain')
return response
```
In this example, the `HttpResponse` object contains the string `'Hello, world!'` as its content, and the `content_type` parameter is set to `'text/plain'` to indicate that the response is plain text.
阅读全文