Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting data from plain HTML form into Django User object

I have created my own plain HTML form and I want to get that data into a view to create the default User object.

However , I am not being able to get the data from the form, here is my view :

def registerPage(request):
    if request.method == "POST":
        print(request.POST.get('name'))
        print(request.POST.get('useremail'))
        username = request.POST.get('name')
        email = request.POST.get('useremail')
        password = request.POST.get('userpassword')
        user = User.objects.create_user(username, email, password)
        return HttpResponse("Printed to the console")
    else:
        return render(request, 'store/register.html')

The console prints "None" as a result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This the HTML :


                  <form class="mx-1 mx-md-4" method="POST" action="http://127.0.0.1:8000/register">
                        {% csrf_token %}
                    <div class="d-flex flex-row align-items-center mb-4">
                      <i class="fas fa-user fa-lg me-3 fa-fw"></i>
                      <div class="form-outline flex-fill mb-0">
                        <input type="text" id="name" class="form-control" />
                        <label class="form-label" for="name">Your Name</label>
                      </div>
                    </div>
  
                    <div class="d-flex flex-row align-items-center mb-4">
                      <i class="fas fa-envelope fa-lg me-3 fa-fw"></i>
                      <div class="form-outline flex-fill mb-0">
                        <input type="email" id="useremail" class="form-control" />
                        <label class="form-label" for="useremail">Your Email</label>
                      </div>
                    </div>
  
                    <div class="d-flex flex-row align-items-center mb-4">
                      <i class="fas fa-lock fa-lg me-3 fa-fw"></i>
                      <div class="form-outline flex-fill mb-0">
                        <input type="password" id="userpassword" class="form-control" />
                        <label class="form-label" for="userpassword">Password</label>
                      </div>
                    </div>
  
                    <div class="d-flex flex-row align-items-center mb-4">
                      <i class="fas fa-key fa-lg me-3 fa-fw"></i>
                      <div class="form-outline flex-fill mb-0">
                        <input type="password" id="form3Example4cd" class="form-control" />
                        <label class="form-label" for="form3Example4cd">Repeat your password</label>
                      </div>
                    </div>
  
  
                    <div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
                      <button type="submit" class="btn btn-primary btn-lg">Register</button>
                    </div>
  
                  </form>

How should I get those values ? Or at least print them out ?

>Solution :

When you use request.POST.get() you have to identify the input whit the input name, not the input id. So, you have to add the tag "name" to your inputs:

<input type="text" id="name" class="form-control" name="name"/>
<input type="email" id="useremail" class="form-control" name="useremail"/>
....
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading