Simao
Simao

Reputation: 581

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.

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 ?

Upvotes: 0

Views: 53

Answers (1)

LaCharcaSoftware
LaCharcaSoftware

Reputation: 1094

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"/>
....

Upvotes: 1

Related Questions