0

我想在 auth_user 表中插入用户详细信息,但它给出了create_user() 的错误,得到了一个意外的关键字参数 'first_name'

表格.py

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from customer_reg.models import Customer

class Registration_Form(ModelForm):
       first_name  = forms.CharField(label=(u'First Name'))
       last_name   = forms.CharField(label=(u'Last Name'))      
       username   = forms.CharField(label=(u'User Name'))
       email      = forms.EmailField(label=(u'Email Address'))
       password   = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))

       class Meta:
              model=Customer
              exclude=('user',)

       def clean_username(self):
                username=self.cleaned_data['username']
                try:
                      User.objects.get(username=username)
                except User.DoesNotExist:
                      return username
                raise forms.ValidationError("The Username is already taken, please try another.")
       def clean_password(self):
                password=self.cleaned_data['password']
                return password

视图.py

from django.contrib.auth.models import User
from customer_reg.models import Customer
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from customer_reg.forms import Registration_Form


def CustomerRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/customer_profile/')
    if request.method == 'POST':
        form = Registration_Form(request.POST)
        if form.is_valid():
            user=User.objects.create_user(first_name=form.cleaned_data['first_name'], last_name=form.cleaned_data['last_name'], username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])

            user.save()

            customer=user.get_profile()
            customer.birthday=form.cleaned_data['birthday']
            customer.website=form.cleaned_data['website']
            customer.store=form.cleaned_data['store']
            customer.welcomemail=form.cleaned_data['welcomemail']
            customer.save()

            return HttpResponseRedirect('/customer_profile/')
        else:
                return render_to_response('customer_register.html',{'form':form} , context_instance=RequestContext(request)) 
    else:
        ''' user is not submitting the form, show them a blank registration form '''

            form = Registration_Form()
            context={'form':form}
            return render_to_response('customer_register.html',context , context_instance=RequestContext(request))

如果我将 views.py 编辑为

user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])

然后它成功运行

我已经尝试过firstnamefirst_name

知道我在哪里做错了

4

2 回答 2

1

create_usermanager 方法只接受三个参数,,username(可选email)和password(可选)。

创建用户后,您可以修改其他字段,然后再次保存。

user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.save()
于 2012-09-14T09:32:31.283 回答
1

如果您希望能够使用管理界面进行注册,则必须更改应用程序中的 admin.py

于 2012-10-09T03:45:28.550 回答