2013-04-17 63 views
1

我收到此錯誤是在/帳號/ EmpReg/
無法解析關鍵詞 '用戶名' 執行項目Django字段錯誤無法將關鍵字'用戶名'解析爲字段。選項包括:companytype,contactno,contactperson,身份證,工作,用戶

FieldError到現場。選項包括:companytype,contactno,contactperson,身份證,工作,用戶

views.py

class User(models.Model): 
    username = models.CharField(_('username'), max_length=30, unique=True) 
    first_name = models.CharField(_('first name'), max_length=30, blank=True) 
    last_name = models.CharField(_('last name'), max_length=30, blank=True) 
    email = models.EmailField(_('e-mail address'), blank=True) 
    password = models.CharField(_('password'), max_length=128) 
    companyname = models.CharField(_('companyname'), max_length=50) 
    usertype = models.CharField(_('usertype'), max_length=30) 
    is_staff = models.BooleanField(_('staff status'), default=False) 
    is_active = models.BooleanField(_('active'), default=True) 
    is_superuser = models.BooleanField(_('superuser status'), default=False) 
    last_login = models.DateTimeField(_('last login'), default=timezone.now) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 
    groups = models.ManyToManyField(Group, verbose_name=_('groups'),blank=True) 
    user_permissions = models.ManyToManyField(Permission,verbose_name=_('user permissions'), blank=True) 


class RegistrationProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    activation_key = models.CharField(max_length=40) 
    key_generated = models.DateTimeField() 
    objects = RegistrationManager() 

forms.py

class EmpRegistrationForm(forms.Form): 
    username = forms.CharField(max_length=30,widget=forms.TextInput) 
    email = forms.EmailField(widget=forms.TextInput)) 
    password1 = forms.CharField(widget=forms.PasswordInput) 
    password2 = forms.CharField(widget=forms.PasswordInput) 
    usertype = forms.CharField(widget=forms.HiddenInput) 
    companyname = forms.CharField(widget=forms.TextInput) 
    companytype = forms.ChoiceField(choices=PREFERRED_COMPANYTYPE, widget=forms.RadioSelect()) 
    contactno = forms.CharField(widget=forms.TextInput) 
    contactperson = forms.CharField(widget=forms.TextInput) 
    tos = forms.BooleanField() 

views.py

def empreg(request): 
    if request.method == 'POST': 
     form = EmpRegistrationForm(request.POST) 
     if form.is_valid(): 
      user = RegistrationProfile.objects.create_inactive_user(user__username=form.cleaned_data['username'],password=form.cleaned_data['password1'],email=form.cleaned_data['email'],) 
      user.companyname = form.cleaned_data['companyname'] 
      user.usertype = form.cleaned_data['usertype'] 
      user.save() 
      e=EmployerReg_Form(user=user, companytype=form.cleaned_data['companytype'],contactno=form.cleaned_data['contactno'],contactperson=form.cleaned_data['contactperson']) 
      e.save() 
      return HttpResponseRedirect('/accounts/EmpReg_Complete/') 
else: 
    form = EmpRegistrationForm() 
    return render_to_response('registration/empregform.html', { 'form': form }, context_instance=RequestContext(request)) 

models.py

class EmployerReg_Form(models.Model): 

    user = models.ForeignKey(User, unique=True) 
    companytype = models.CharField(max_length=20) 
    contactno = models.CharField(max_length=30) 
    contactperson = models.CharField(max_length=30) 

PREFERRED_COMPANYTYPE = (
('Company', 'Company'), 
('Consultancy', 'Consultancy'), 
) 

請查看上面的代碼,並幫助我哪裏錯

+0

什麼是「EmployerReg_Form」,它看起來如何? –

+0

EmployerReg_Form是一個模型,現在我更新了 – user2251503

+0

還有一些缺失。相關型號不包含在您的問題中... –

回答

1

在這一部分,你在呼喚EmployerReg_Form:

e=EmployerReg_Form(
     user=user, 
     companytype=form.cleaned_data['companytype'], 
     contactno=form.cleaned_data['contactno'], 
     contactperson=form.cleaned_data['contactperson'] 
) 

當你看該模型將會看到用戶字段是用於爲用戶建模的ForeignKey。如果你知道ID EmployerReg_Form應該被映射到用戶的線路是:

e=EmployerReg_Form(
     user__ID=user, 
     companytype=form.cleaned_data['companytype'], 
     contactno=form.cleaned_data['contactno'], 
     contactperson=form.cleaned_data['contactperson'] 
) 

映射到一個名稱將是這樣的:

e=EmployerReg_Form(
     user__username=user, 
     companytype=form.cleaned_data['companytype'], 
     contactno=form.cleaned_data['contactno'], 
     contactperson=form.cleaned_data['contactperson'] 
) 

注意用戶後雙下劃線。

哦,不應該用戶模型在您的models.py而不是views.py?

+0

謝謝你的回答。我將僱主和求職者的詳細信息存儲在同一個用戶表中。我使用字段usertype(usertype = employer/jobseeker)區分了兩個細節。雖然登錄後身份驗證,它應該重定向到儀表板取決於他們的用戶類型。如果用戶是求職者,它應該被重定向到一個儀表板,否則用戶是僱主,它應該被重定向到另一個儀表板頁面。請幫幫我。 – user2251503

相關問題