在我的注册表单上添加字段

时间:2014-10-23 16:11:07

标签: python django django-forms django-registration user-registration

我有一个完整功能的注册表,实际上包含用户名,电子邮件,密码和确认。我想添加新字段 Telefono (英语电话,借口)全名

我正在尝试这样做但是有一个问题:

'fullname' is an invalid keyword argument for this function

我相信Telefono领域也有同样的问题。我真的在views.py和forms.py上添加这些字段。请看我的配置:

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')
如果我忽略某些事情,请提前道歉。

谢谢!

1 个答案:

答案 0 :(得分:2)

您无法添加它们,因为它们不存在于内置User模型中。

请参阅此处了解如何使用您自己的User模型:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model

建议您创建自己的,但您也可以将django.contrib.auth.models.AbstractUser子类化为扩展内置的。{/ p>