创建Django的自定义用户注册表 [英] Creating Custom user registration form Django

查看:139
本文介绍了创建Django的自定义用户注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django中创建自定义用户注册表单,但我收到以下错误。我的页面上的所有内容都会正确显示,但是我收到错误。



错误:

 异常类型:KeyError 
异常值:'名字'

我的form.py:


$ b从django导入表单$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 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 =用户
fields =('username','email','password1','password2')

def save(self,commit = True):
user = 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 ['姓氏]
user.birthday = self.cleaned_data ['Birthday']


如果提交:
user.save()

返回用户

我的views.py

  from django.shortcuts import render 
from django。从django.contrib导入httpResponseRedirect
从django.core.context_processors导入auth
从表单导入导入csrf
MyRegistrationForm

def register_user(request):
如果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)


解决方案

这里是问题,您使用标签访问字段,而应该通过表单字段名称访问:

  self.cleaned_data ['First name'] 

应该是

  self.cleaned_data ['first_name'] 

同样 last_name 生日


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)

解决方案

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.

这篇关于创建Django的自定义用户注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆