Easy Tutorial
❮ Django Template Django Middleware ❯

Django Views

View Layer

A view function, or view for short, is a simple Python function that takes a web request and returns a web response.

The response can be an HTML page, a 404 error page, a redirect page, an XML document, or an image...

Regardless of the logic contained within the view, it must return a response. The code can be written anywhere as long as it is within the Python directory, typically placed in the project's views.py file.

Each view function is responsible for returning an HttpResponse object, which contains the generated response.

There are two important objects in the view layer: the request object (request) and the response object (HttpResponse).


Request Object: HttpRequest Object (referred to as request object)

Here are a few commonly used request attributes.

1. GET

The data type is QueryDict, a dictionary-like object containing all HTTP GET parameters.

If there are duplicate keys, all values are placed in a corresponding list.

Access format: object.method.

get(): Returns a string. If the key has multiple values, it returns the last value of the key.

Example

def tutorialpro(request):
    name = request.GET.get("name")
    return HttpResponse('Name: {}'.format(name))

2. POST

The data type is QueryDict, a dictionary-like object containing all HTTP POST parameters.

Commonly used for form submissions, where the name attribute of the form tag corresponds to the parameter key, and the value attribute corresponds to the parameter value.

Access format: object.method.

get(): Returns a string. If the key has multiple values, it returns the last value of the key.

Example

def tutorialpro(request):
    name = request.POST.get("name")
    return HttpResponse('Name: {}'.format(name))

3. body

The data type is binary byte stream, containing the raw parameter content from the request body. Used in HTTP for POST requests, as GET requests do not have a body.

Less commonly used in HTTP, but very useful for handling non-HTTP message formats, such as binary images, XML, JSON, etc.

Example

def tutorialpro(request):
    name = request.body
    print(name)
    return HttpResponse("tutorialpro.org")

4. path

Retrieves the path part of the URL. The data type is a string.

Example

def tutorialpro(request):
    name = request.path
    print(name)
    return HttpResponse("tutorialpro.org")

5. method

Retrieves the current request method. The data type is a string, and the result is in uppercase.

Example

def tutorialpro(request):
    name = request.method
    print(name)
    return HttpResponse("tutorialpro.org")

Response Object: HttpResponse Object

There are three main forms of response objects: HttpResponse(), render(), and redirect().

HttpResponse(): Returns text. The parameter is a string containing the text content. If the string contains HTML tags, they can also be rendered.

Example

def tutorialpro(request):
    # return HttpResponse("tutorialpro.org")
    return HttpResponse("<a href='https://www.tutorialpro.org/'>tutorialpro.org</a>")

render(): Returns text. The first parameter is request, the second parameter is a string (page name), and the third parameter is a dictionary (optional, parameters passed to the page: key is the page parameter name, value is the view parameter name).

Example

def tutorialpro(request):
    name = "tutorialpro.org"
    return render(request, "tutorialpro.html", {"name": name})

redirect(): Redirects to a new page. The parameter is a string containing the page path. Typically used after form submission to redirect to a new page.

Example

def tutorialpro(request):
    return redirect("https://www.tutorialpro.org/")
def tutorialpro(request):
    return redirect("/index/")

Both render and redirect are encapsulations built on top of HttpResponse:

❮ Django Template Django Middleware ❯