Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:26:08+00:00 2026-06-10T12:26:08+00:00

when I am submitting the form, I am getting KeyError at password1 in forms.py

  • 0

when I am submitting the form, I am getting KeyError at password1 in forms.py line 25 password1=self.cleaned_data[‘password1’]. The code of files is given below:

The code of forms.py is:

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from drinker.models import Drinker

class RegistrationForm(ModelForm):
       username   = forms.CharField(label=(u'User Name'))
       email      = forms.EmailField(label=(u'Email Address'))
       password   = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
       password1   = forms.CharField(label=(u'Verify Password'), widget=forms.PasswordInput(render_value=False))

       class Meta:
              model=Drinker
              exclude=('user',)

       def clean_username(self):
                username=self.cleaned_data['username']
                try:
                      User.objects.get(username=username)
                except User.DoesNotExist:
                      return username
                raise forms.ValidationError("The Username is already taken, please try another.")
       def clean_password(self):
                password=self.cleaned_data['password']
                password1=self.cleaned_data['password1']
                if password != password1:
                    raise forms.ValidationError("The Password did not match, please try again.")
                return password

The code of html file is:

{% extends "base.html" %}
{% block extrahead %}
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js" type="text/javascript"></script>
    <script>
    $(function() {
        $( "#id_birthday" ).datepicker();
    });
    </script>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
<div class="register_div">
    {% if form.username.errors %}<p class="error">{{ form.username.errors }}</p>{% endif %}
    <p><label for="username"{% if form.username.errors %} class="error"{% endif %}>Username:</label></p>
    <p>{{ form.username }}</p>
</div>
<div class="register_div">
    {% if form.email.errors %}<p class="error">{{ form.email.errors }}</p>{% endif %}
    <p><label for="email"{% if form.email.errors %} class="error"{% endif %}>Email:</label></p>
    <p>{{ form.email }}</p>
</div>
<div class="register_div">
    {% if form.password.errors %}<p class="error">{{ form.password.errors }}</p>{% endif %}
    <p><label for="password"{% if form.password.errors %} class="error"{% endif %}>Password:</label></p>
    <p>{{ form.password }}</p>
</div>
<div class="register_div">
    {% if form.password1.errors %}<p class="error">{{ form.password1.errors }}</p>{% endif %}
    <p><label for="password1"{% if form.password1.errors %} class="error"{% endif %}>Password1:</label></p>
    <p>{{ form.password1 }}</p>
</div>
<div class="register_div">
    {% if form.birthday.errors %}<p class="error">{{ form.birthday.errors }}</p>{% endif %}
    <p><label for="birthday"{% if form.birthday.errors %} class="error"{% endif %}>Birthday:</label></p>
    <p>{{ form.birthday }}</p>
</div>
<div class="register_div">
    {% if form.name.errors %}<p class="error">{{ form.name.errors }}</p>{% endif %}
    <p><label for="name"{% if form.name.errors %} class="error"{% endif %}>Name:</label></p>
    <p>{{ form.username }}</p>
</div>
<p><input type="submit" value="submit"/></p>
</form>
{% endblock %}

the view file is

from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from drinker.models import Drinker
from django.template import RequestContext
from drinker.forms import RegistrationForm

def DrinkerRegistration(request):
    if request.user.is_authenticated():
         return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
         #return render_to_response('register')
         form = RegistrationForm(request.POST)
         if form.is_valid():
                  user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])

                  user.save()
 #                 drinker=user.get_profile()
  #                drinker.name=form.cleaned_data['name']
   #               drinker.birthday=form.cleaned_data['birthday']
    #              drinker.save()
                 drinker=Drinker(user=user,name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday'])  
                  drinker.save()    
                  return HttpResponseRedirect('/profile/')
         else:
                  return render_to_response('register.html',{'form':form} , context_instance=RequestContext(request))  
    else:
         ''' user is not submitting the form, show them a blank registration form '''

         form = RegistrationForm()
         context={'form':form}
         return render_to_response('register.html',context , context_instance=RequestContext(request))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:26:09+00:00Added an answer on June 10, 2026 at 12:26 pm

    Django calls clean_xxx() methods for each field in the form. When a field is valid, it will add the key/value to cleaned_data. When Django calls clean_password and it has not yet processed the field password1 then cleaned_data won’t contain a key/value pair for password1. That is the error you are currently getting.

    To fix it, you need to move the validation of the passwords to clean() which is called after Django has checked each of the field individually. This method can be used to perform validation which concerns multiple fields of the form (i.e., the form as a whole).

    This also makes sense because the error you want to raise is about the mismatch between password and password1, not just the field password (the field that you are currently cleaning).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting Error in submitting the form using AJAX and Jquery, following is
When submitting the following form I am getting this error : Fatal error: Call
I am submitting from a form to another php page which is supposed to
I'm getting an ActiveRecord error after submitting a form for a table called worequests.
I am submitting some form data via ajax and getting back a JSON array
I am getting an Undefined index error when submitting a form with an un-checked
I'm using OneToOneField connected with User to do form submitting tasks. But I'm getting
I am submitting a form and getting JSON as a respond. My goal is
I am having some trouble getting a button clicked after submitting a form using
How do I prevent my user from submitting an empty form? When I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.