0

I created a signup page but when I tried to signup it gives a MultiValueDictKeyError error

my HTML looks like

<form action="/signup" method="post">
        <div class="form-group">
          <label for="username">User Name</label>
          <input type="text" class="form-control" id="username" placeholder="user name">
        </div>

        <div class="form-group">
          <label for="fname">First Name</label>
          <input type="text" class="form-control" id="fname" placeholder="First name">
        </div>

        <div class="form-group">
          <label for="lname">Last Name</label>
          <input type="text" class="form-control" id="lname" placeholder="Last name">
        </div>

        <div class="form-group">
          <label for="email">Email address</label>
          <input type="email" class="form-control" id="email" placeholder="name@example.com">
        </div>
        <div class="form-group">
          <label for="pass1">Password</label>
          <input type="password" class="form-control" id="pass1" placeholder="Enter a password">

        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          {%csrf_token%}
          <button type="submit" class="btn btn-primary">Submit</button>
        </div>

      </form>

here is my views.py

def handleSignup(request):
if request.method == 'POST':
    #get the post parameters
    username = request.POST['username']
    fname = request.POST['fname']
    lname = request.POST['lname']
    email = request.POST['email']
    pass1 = request.POST['pass1']


    #check for error inputs
    # create the user
    myuser = User.objects.create_user(username,email,pass1)
    myuser.first_name = fname
    myuser.last_name = lname
    myuser.save()
    messages.success(request,"account Created")
    return redirect('home')

URLs are:

urlpatterns = [

    path('', views.home, name='home'),
    path('about', views.about, name='about'),
    path('contact', views.contact, name='contact'),
    path('search', views.search, name='search'),
    path('signup', views.handleSignup, name='handleSignup')
]

and the error looks like

MultiValueDictKeyError at /signup 'username' Request Method: POST Request URL: http://127.0.0.1:8000/signup Django Version: 3.0.6 Exception Type: MultiValueDictKeyError Exception Value:
'username' Exception Location: /home/tanmoy/.local/lib/python3.6/site-packages/django/utils/datastructures.py in getitem, line 78 Python Executable: /usr/bin/python3 Python Version: 3.6.9 Python Path:
['/home/tanmoy/Desktop/djangoProjects/codingblog', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/tanmoy/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/local/lib/python3.6/dist-packages/object_detection-0.1-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/Cython-0.29.15-py3.6-linux-x86_64.egg', '/usr/local/lib/python3.6/dist-packages/matplotlib-3.2.0-py3.6-linux-x86_64.egg', '/usr/local/lib/python3.6/dist-packages/pyparsing-2.4.6-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/kiwisolver-1.1.0-py3.6-linux-x86_64.egg', '/usr/local/lib/python3.6/dist-packages/cycler-0.10.0-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/slim-0.1-py3.6.egg', '/usr/lib/python3/dist-packages'] Server time: Thu, 28 May 2020 18:48:05 +0000

Tanmoy Bhowmick
  • 1,305
  • 15
  • 20

0 Answers0