YouTip LogoYouTip

Django Views

### View Layer\n\nA view function, simply called a view, is a simple Python function that accepts a Web request and returns a Web response.\n\nThe response can be an HTML page, a 404 error page, a redirect page, an XML document, or an image...\n\nRegardless of what logic the view itself contains, it must return a response. The code can be written anywhere, as long as it's within the Python directory, typically placed in the project's `views.py` file.\n\nEach view function is responsible for returning an `HttpResponse` object, which contains the generated response.\n\nThere are two important objects in the view layer: the request object (`request`) and the response object (`HttpResponse`).\n\n* * *\n\n## Request Object: The HttpRequest Object (referred to as the `request` object)\n\nThe following introduces some commonly used `request` attributes.\n\n### 1. GET\n\nThe data type is `QueryDict`, a dictionary-like object containing all parameters from the HTTP GET request.\n\nIf there are duplicate keys, all values are placed into a corresponding list.\n\nAccess format: `object.method`.\n\n`get()`: Returns a string. If the key has multiple values, it retrieves the last value for that key.\n\n## Example\n\n```python\ndef (request):\n name = request.GET.get("name")\n return HttpResponse('Name:{}'.format(name))\n\n!(#)\n\n### 2. POST\n\nThe data type is `QueryDict`, a dictionary-like object containing all parameters from the HTTP POST request.\n\nCommonly used with form elements, where the `name` attribute of a form tag corresponds to the parameter's key, and the `value` attribute corresponds to the parameter's value.\n\nAccess format: `object.method`.\n\n`get()`: Returns a string. If the key has multiple values, it retrieves the last value for that key.\n\n## Example\n\n```python\ndef (request):\n name = request.POST.get("name")\n return HttpResponse('Name:{}'.format(name))\n\n!(#)\n\n### 3. body\n\nThe data type is a binary byte stream, containing the raw content of the request body. In HTTP, this is used for POST requests, as GET requests do not have a body.\n\nIt is not commonly used in HTTP but is very useful when processing non-HTTP message formats, such as binary images, XML, JSON, etc.\n\n## Example\n\n```python\ndef (request):\n name = request.body\n print(name)\n return HttpResponse("")\n\n!(#)\n\n### 4. path\n\nRetrieves the path portion of the URL. The data type is a string.\n\n## Example\n\n```python\ndef (request):\n name = request.path\n print(name)\n return HttpResponse("")\n\n!(#)\n\n### 5. method\n\nRetrieves the HTTP method of the current request. The data type is a string, and the result is in uppercase.\n\n## Example\n\n```python\ndef (request):\n name = request.method\n print(name)\n return HttpResponse("")\n\n!(#)\n\n* * *\n\n## Response Object: The HttpResponse Object\n\nThe response object has three main forms: `HttpResponse()`, `render()`, and `redirect()`.\n\n**HttpResponse():** Returns text. The parameter is a string containing the text content. If the string contains HTML tags, it can also be rendered.\n\n## Example\n\n```python\ndef (request):\n # return HttpResponse("")\n return HttpResponse("")\n\n!(#)\n\n**render():** Returns text. The first parameter is `request`, the second is a string (the page name), and the third is a dictionary (an optional parameter to pass to the page: keys are the page parameter names, values are the view parameter names).\n\n## Example\n\n```python\ndef (request):\n name = ""\n return render(request, ".html", {"name": name})\n\n!(#)\n\n**redirect():** Redirects to a new page. The parameter is a string containing the page path. It is generally used after a form submission to jump to a new page.\n\n## Example\n\n```python\ndef (request):\n return redirect("/index/")\n\n`render` and `redirect` are encapsulations based on `HttpResponse`:\n\n* `render`: Under the hood, it also returns an `HttpResponse` object.\n* `redirect`: Under the hood, it inherits from the `HttpResponse` object.
← Os StatvfsOs Rmdir β†’