Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Custom user registration form Django

Tags:

python

django

I am trying to create custom user registration forms in Django but I am getting the following error. Everything on my page displays correctly however I get the error.

Error:

Exception Type: KeyError
Exception Value:    'First name'

My form.py:

from django import forms            
from django.contrib.auth.models import User   # fill in custom user info then save it 
from django.contrib.auth.forms import UserCreationForm      

class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required = True)
    first_name = forms.CharField(required = False)
    last_name = forms.CharField(required = False)
    birtday = forms.DateField(required = False)



    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')        

    def save(self,commit = True):   
        user = super(MyRegistrationForm, self).save(commit = False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['First name']
        user.last_name = self.cleaned_data['Last name']
        user.birthday = self.cleaned_data['Birthday']


        if commit:
            user.save()

        return user

My views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect    
from django.contrib import auth                 
from django.core.context_processors import csrf 
from forms import MyRegistrationForm

def register_user(request):
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)     # create form object
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')
    args = {}
    args.update(csrf(request))
    args['form'] = MyRegistrationForm()
    print args
    return render(request, 'register.html', args)
like image 995
Liondancer Avatar asked Nov 25 '13 11:11

Liondancer


People also ask

Should you create custom user model in Django?

Every new Django project should use a custom user model. The official Django documentation says it is “highly recommended” but I'll go a step further and say without hesitation: You are straight up crazy not to use a custom user model up front.

How can I send mail after registration in Django?

Configure Settings First we need to configure the email host server in the settings.py for confirmation mail. Add the below configuration in the settings.py file. We used the email-id along with the password and gmail SMTP host server. You can use the other SMTP server as well.

How do I authenticate username and password in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...


1 Answers

Here is the problem, you are accessing fields by using label rather it should be accessed by form field name:

self.cleaned_data['First name']

should be

self.cleaned_data['first_name']

Similarly last_name and birthday.

like image 53
Aamir Rind Avatar answered Nov 06 '22 02:11

Aamir Rind