Passing values via post with Django

17,018

Solution 1

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.

Solution 2

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')
Share:
17,018
Chris
Author by

Chris

product and software at curative, scaling COVID-19 testing nationwide prior: senior forward deployed software engineer — Elizabeth Warren for President at Reach head of batch, XX CEO, seneca systems principal software engineer, zenpayroll (now Gusto) software engineer, LivingSocial

Updated on June 04, 2022

Comments

  • Chris
    Chris almost 2 years

    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)?

  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 12 years
    The word you're looking for is "mapping".
  • Chris
    Chris over 12 years
    Okay, so I used that, but now it says that password isn't part of the POST request, but it does appear to have email, username, and something funky called csrfmiddlewaretoken. Why wouldn't it send the password field of the form as well?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 12 years
    Because you forgot to give it a name. Which is yet another reason to use Django forms.
  • Chris
    Chris over 12 years
    You're too good. I will look into the form class, just wanted to make it through the damn tutorial first haha. Thanks a bunch!
  • Soren
    Soren about 3 years
    But how to populate POST?