0

auth_userテーブルにユーザーの詳細を挿入したかったのですが、create_user ()が予期しないキーワード引数'first_name'を取得したというエラーが発生します。

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

その後、正常に動作します

私はすでにfirstnamefirst_nameを試しました

私が間違いをした場所のアイデア

4

2 に答える 2

1

managerメソッドは、、(オプション)、および(オプション)のcreate_user3つの引数のみを受け入れます。usernameemailpassword

ユーザーを作成したら、他のフィールドを変更して、再度保存できます。

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 に答える