Search code examples
pythondjangodjango-formsdjango-registrationuser-registration

Adding field on my register form


I have a register form fully functional that actually contain Username, Email, Pass&confirmation. I wanna add new fields Telefono (phone on english, excuseme) and fullname.

I was trying do it but there was a problem:

'fullname' is an invalid keyword argument for this function

I believe that Telefono field has the same problem. I really add these fields on views.py and forms.py. please look my configuration:

views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
from dracoin.apps.synopticup.models import card
from dracoin.apps.home.forms import ContactForm,LoginForm,RegisterForm
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth.models import User

from django.contrib.auth import login,logout,authenticate
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, InvalidPage

def register_view(request):
    form = RegisterForm()
    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            fullname = form.cleaned_data['fullname']
            usuario = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password_one = form.cleaned_data['password_one']
            password_two = form.cleaned_data['password_two']
            telefono = form.cleaned_data['telefono']
            u = User.objects.create_user(fullname=fullname,username=usuario,email=email,password=password_one,telefono=telefono)
            u.save()
            return render_to_response('home/thanks_register.html',context_instance=RequestContext(request))
        else:
            ctx = {'form':form}
            return render_to_response('home/register.html',ctx,context_instance=RequestContext(request))
    ctx = {'form':form}
    return render_to_response('home/register.html',ctx,context_instance=RequestContext(request))

forms.py:

from django import forms
from django.contrib.auth.models import User

class RegisterForm(forms.Form):
    fullname = forms.CharField(label="Nombre completo",widget=forms.TextInput())
    username = forms.CharField(label="Nombre de usuario",widget=forms.TextInput())
    email = forms.EmailField(label="Correo Electronico",widget=forms.TextInput())
    password_one = forms.CharField(label="Password",widget=forms.PasswordInput(render_value=False))
    password_two = forms.CharField(label="Confirmar password",widget=forms.PasswordInput(render_value=False))
    telefono = forms.RegexField(regex=r'^\+?1?\d{9,15}$', 
                                error_message = ("Only celular phone number. Must be entered in the format: '+999999999'. Up to 15 digits allowed."))
    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            u = User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError('Nombre de usuario ya existe')

    def clean_email(self):
        email = self.cleaned_data['email']
        try:
            u = User.objects.get(email=email)
        except User.DoesNotExist:
            return email
        raise forms.ValidationError('Email ya registrado')

    def clean_password_two(self):
        password_one= self.cleaned_data['password_one']
        password_two= self.cleaned_data['password_two']
        if password_one == password_two:
            pass
        else:
            raise forms.ValidationError('passwords no coincidentes')

apologizeme in advance if I overlook something.

Thanks!!


Solution

  • You can't just add them because they don't exist on the built-in User model.

    See here on how to use your own User model: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model

    It is recommended to create your own, but you can also just subclass django.contrib.auth.models.AbstractUser to extend the built in one.