I am trying to save a user with the generated password and username, and I have small problems.
My view.py looks like this:
...
if request.method == "POST":
u = get_username(request.POST.get('first_name', '').lower() + "." + request.POST.get('last_name', '').lower())
p = User.objects.make_random_password(length = 6)
form = PrincipalRegistrationForm(request.POST, request.FILES)
if form.is_valid():
...
new_user = form.save(uname = u, pword = p)
return HttpResponseRedirect("/path/to/site")
...
and my forms.py like this:
class PrincipalRegistrationForm(UserCreationForm ):
first_name = forms.CharField(label = 'First name')
last_name = forms.CharField(label = 'Last name')
school = forms.ModelChoiceField(queryset = School.objects.all())
profile_pic = forms.FileField(required=False)
def save(self,commit = True, uname = "unkown", pword = "unknown"):
user = super(PrincipalRegistrationForm, self).save(commit = False)
user.username = uname
user.set_password = make_password(pword)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
return user
def __init__(self, *args, **kwargs):
super(PrincipalRegistrationForm, self).__init__(*args, **kwargs)
self.fields.pop('username')
self.fields.pop('password1')
self.fields.pop('password2')
The username is generated by an auxiliary function and consists of fname and lname and sent to the save function, as in the code, it works fine (if I type the password manually). Now I want the password to be generated and saved as the username, but I have problems with this. I get the following errors:
Exception Type: KeyError
Exception Value: u'password1'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py in save, line 121
Thanks for any help, and I appreciate any advice on how I should write code or change something.
Traceback:
Environment:
Request Method: POST
Request URL: http://localhost:8000/siteadmin/add/principal
Django Version: 1.7.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_accounts',
'app_schools',
'app_school_subjects')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/path/to/ednevnik/app_accounts/views.py" in add_principal
80. new_user = form.save(uname = u, pword = p)
File "/path/to/ednevnik/app_accounts/forms.py" in save
64. user = super(PrincipalRegistrationForm, self).save(commit = False)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py" in save
121. user.set_password(self.cleaned_data["password1"])
Exception Type: KeyError at /siteadmin/add/principal
Exception Value: u'password1'
It is decided:
def save(self, commit = True, uname = "unkown", pword = "unknown"):
self.cleaned_data['password1'] = pword
user = super(PrincipalRegistrationForm, self).save(commit = False)
user.username = uname
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
return user