Home > Enterprise >  Getting data from plain HTML form into Django User object
Getting data from plain HTML form into Django User object

Time:02-21

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  method="POST" action="http://127.0.0.1:8000/register">
                        {% csrf_token %}
                    <div >
                      <i ></i>
                      <div >
                        <input type="text" id="name"  />
                        <label  for="name">Your Name</label>
                      </div>
                    </div>
  
                    <div >
                      <i ></i>
                      <div >
                        <input type="email" id="useremail"  />
                        <label  for="useremail">Your Email</label>
                      </div>
                    </div>
  
                    <div >
                      <i ></i>
                      <div >
                        <input type="password" id="userpassword"  />
                        <label  for="userpassword">Password</label>
                      </div>
                    </div>
  
                    <div >
                      <i ></i>
                      <div >
                        <input type="password" id="form3Example4cd"  />
                        <label  for="form3Example4cd">Repeat your password</label>
                      </div>
                    </div>
  
  
                    <div >
                      <button type="submit" >Register</button>
                    </div>
  
                  </form>

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

CodePudding user response:

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"  name="name"/>
<input type="email" id="useremail"  name="useremail"/>
....
  • Related