How to get POST request values in Django Views?

Question:

I’m trying to make a signup form via html/django so I have 3 input boxes for the user to put in the email, username, and password that then sends them via POST to /adduser

<form action="/OmniCloud_App/adduser" method="post">
{% csrf_token %}
  Email Address: <input type="text" name="email" /></br>
  Username: <input type="text" name="username" maxlength=25 /></br>
  Password: <input type="password" maxlength=30 /></br>
  </br>
  <input type="submit" value="Send" /> <input type="reset">
</form>

adducer creates a new User and saves it to the DB:

def adduser(request, email, username, password):
    u = User(email=email, username=username, password=password)
    u.save()
    return render_to_response('adduser.html', {'email':email, 'username':username, 'password':password})

but when I click submit on /signup, it complains that I am only giving it 1 parameter when 3 were expected. How should I pass the email,username, and password fields from signup.html to the username function (located at /username)?

Asked By: Chris

||

Answers:

they will be in request.POST, which you can query like you would a dict

email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
Answered By: Roshan Mathews

If you read part 3 of the tutorial, you’ll see that the view function expects parts of the URL itself as arguments. If you read part 4 of the same tutorial, you’ll see that POST parameters come in via request.POST. Further in the documentation, you’ll learn that you can write Form classes that handle both the generation and validation of HTML forms.

For example, if you submit the POST request values in index.html as shown below:

{# "index.html" #}

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple" /></br>
  <input type="text" name="meat" value="beef" /></br>
  <input type="submit" />
</form>

Then, you can get the POST request values in my_app1/views.py as shown below. *My answer explains it more:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits']) # apple
    print(request.POST.get('meat')) # beef
    print(request.POST.get('fish')) # None
    print(request.POST.get('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST.getlist('fruits')) # ['apple']
    print(request.POST.getlist('fish')) # []
    print(request.POST.getlist('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('meat')) # ['beef']
    print(request.POST._getlist('fish')) # []
    print(request.POST._getlist('fish', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.keys())) # ['csrfmiddlewaretoken', 'fruits', 'meat']
    print(list(request.POST.values())) # ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'apple', 'beef']
    print(list(request.POST.items())) # [('csrfmiddlewaretoken', 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'), ('fruits', 'apple'), ('meat', 'beef')]
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS']), ('fruits', ['apple']), ('meat', ['beef'])]
    print(request.POST.dict()) # {'csrfmiddlewaretoken': 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'fruits': 'apple', 'meat': 'beef'}
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'], 'fruits': ['apple'], 'meat': ['beef']}

    return render(request, 'test.html')