Skip to content

Sending and receiving data

Anders Kvamme Felde edited this page Apr 6, 2021 · 1 revision

Sending and receiving data

Receiving data

GET

This is the example form which the user fills out

<form>
    {% csrf_token %}
    <input name="email" type="email">
    <input name="password" type="password">
    <button type="submit">Submit</button>
</form>

When the user hits the Submit-button, the user will be redirected to a URL that looks like this:

The csrf_token is mandatory in Django. It is a security-feature to prevent XXS-attacks.

http://localhost/form.html?email=joe@mama.com&password=password123

What the user typed in to the fields will be represented in the URL. To get the data in python:

if "password" in request.GET:
    email = request.GET.get("email")
    password = request.GET.get("password")

In the first line we check if the "password" parameter exists in the URL. If it does it gets value of the "email" and "password" parameters.

POST

The same form will be used, except the first line is changed to:

<form method="POST">

When using POST-method in a form the data is no longer represented in the URL, but in the request itself. To get the values in python:

if request.method == "POST":
    email = request.POST.get("email")
    password = request.POST.get("password")

The first line check if the method of the request is "POST", which indicates that the user has submitted the form.

The default request method is GET

Sending data

Let's say you now want to send confirmation to the user that you received their email and password. We have now stored the values in variables, but want to represent them in the HTML.

To send data to the HTML-template use the context parameter in the render-function:

The parameter requires a dictionary

return render(request,
          "webpage/formPage.html",
          context={"email": email, "passwordLength": len(password)})

In the HTML write:

{% if email %}
<p>
    Thank you for signing up: {{ email }} <br>
    Your password is {{ passwordLength }} characters long
</p>
{% endif %}

The first line checks if the email variable is set, if so it prints the value of email and passwordLength

Clone this wiki locally