2013-04-11 70 views
3

我有一个名为Employee的模型用于注册。当我运行代码我去Django:TypeError:'username'是此函数的无效关键字参数

username' is an invalid keyword argument for this function 

错误。

employee=Employee(user=user, username= form.cleaned_data['username'], email= form.cleaned_data['email'], password=form.cleaned_data['password']) 

这是我view.py代码

from django.http import HttpResponseRedirect 
from django.contrib.auth.models import User 
from django.shortcuts import render_to_response 
from django.template import RequestContext 
from Employee_Details.forms import RegistationForm 
from Employee_Details.models import Employee 

def EmployeeRegistation(request): 
if request.user.is_authenticated(): 
    return HttpResponseRedirect('/profile/') 

if request.method=='POST': 
    form=RegistationForm(request.POST) 
    if form.is_valid(): 
     user=User.objects.create_user(username= form.cleaned_data['username'],email= form.cleaned_data['email'],password=form.cleaned_data['password']) 


     user.save() 

     employee=Employee(user=user, username= form.cleaned_data['username'],email= form.cleaned_data['email'],password=form.cleaned_data['password']) 
     employee.save() 
     return HttpResponseRedirect('/profile/') 

    else: 
     return render_to_response('registation.html',{"form":form}, context_instance=RequestContext(request)) 


else: 
    '''user is not submitting the form show a blank Registation Form''' 
    form=RegistationForm(); 
    context={'form':form} 
    return render_to_response('registation.html',context,context_instance=RequestContext(request)) 

这是我的模型:

from django.db import models 
from django.contrib.auth.models import User 
from django.db.models.signals import post_save 

class Employee(models.Model): 
user=models.OneToOneField(User) 
name=models.CharField(max_length=100) 
address=models.CharField(max_length=200) 
designation=models.CharField(max_length=100) 
email=models.CharField(max_length=100) 
role=models.CharField(max_length=10) 
#image=models.ImageField() 
project=models.CharField(max_length=50) 
task=models.CharField(max_length=50) 

def __unicode(self): 

    return self.name 

这是我form.py

from django import forms 
from django.contrib.auth.models import User 
from django.forms import ModelForm 
from Employee_Details.models import Employee 

class RegistationForm(ModelForm): 

username=forms.CharField(label=(u'User Name')) 
email=forms.EmailField(label=(u'Email Address')) 
password=forms.CharField(label=(u'Password')) 
password1=forms.CharField(label=(u'Verfy Password'))  
name=forms.CharField(label=(u'Name')) 
address=forms.CharField(label=(u'Address')) 
designation=forms.CharField(label=(u'Designation')) 
role=forms.CharField(label=(u'Role')) 
#image=models.ImageField() 
project=forms.CharField(label=(u'Project')) 
task=forms.CharField(label=(u'Task')) 

class Meta: 
    model=Employee 
    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 with another') 




def clean(self): 

    if self.cleaned_data['password'] !=self.cleaned_data['password1']: 
     raise forms.ValidationError("The Password did not match. Please try again") 
    return self.cleaned_data  

请别人帮帮忙我解决这件事。我指的是一些视频教程。

+1

您的员工型号请 – catherine 2013-04-11 07:28:23

+0

请添加您的型号代码。 – 2013-04-11 07:33:16

+0

我编辑了我的代码。请参考 – Lahiruzz 2013-04-11 07:36:04

回答

4

你没有username场模型,并因为你有那场它是多余的已经在User模式:

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

employee=Employee(user=user, email=form.cleaned_data['email'], password=form.cleaned_data['password']) 
(...) 
+0

现在当我在我的url中输入http://127.0.0.1:8000/register时,它会自动重定向到http://127.0.0.1:8000/profile/。这是什么原因? – Lahiruzz 2013-04-11 07:56:33

+0

很可能Django正在重定向你,因为你已经登录了。请参阅:https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url – 2013-04-11 08:55:28

相关问题