2012-09-14 42 views
0

我想插入AUTH_USER表中的用户的详细信息,但它给create_user的误差()得到了一个意想不到的关键字参数“FIRST_NAME”与AUTH_USER表问题

forms.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 

views.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']) 

然后它成功

我已经尝试以及FIRST_NAME

,我也做了错误

任何想法

回答

1

create_user管理方法只接受三个参数,username,email(可选),一个d password(可选)。

创建用户后,您可以修改other fields,然后再次保存。

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() 
+0

得到了感谢! – user786

+0

**客户匹配查询不存在。**以及解决此错误 – user786

+0

的情况。感谢您的时间 – user786

1

如果你希望能够使用管理界面来注册你得改变了admin.py应用内的

+0

我可以添加自己的模板到管理面板,从数据库中获取数据并将其显示给管理员...如果是的话请举个例子,我怎么能看到我的管理面板 – user786

+0

我想你可以在这里找到你的答案:http://stackoverflow.com/questions/10213020/extending-the-user-profile-in-django-admin-creation-of-users – lborgav