3

在解决这个问题几天后,我需要你的帮助!

在扩展 django 注册表单时,我需要两种不同的注册表单。

我的 urls.py :

from django.conf.urls import patterns, url, include
from views import RegistrationViewCandidate, RegistrationViewCompany

urlpatterns = patterns('',
        url(r'accounts/register/company$', RegistrationViewCompany.as_view(), name='registration_company'),
        url(r'accounts/register/candidate$', RegistrationViewCandidate.as_view(), name='registration_candidate'),
        url(r'accounts/', include('registration.backends.default.urls')),
)

我的意见.py:

# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse_lazy as _
from registration.forms import RegistrationForm
from registration.views import RegistrationView
from models import Candidate, Company
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field, Fieldset
from crispy_forms.bootstrap import (PrependedText, PrependedAppendedText, FormActions)


class CandidateForm(forms.ModelForm):

    class Meta:
        model = Candidate
        fields = ('first_name', 'last_name', 'candidate_city', 'candidate_region', 'candidate_phone',
                   'candidate_birthday','candidate_website_url', 'candidate_github_url')


class CompanyForm(forms.ModelForm):

    class Meta:
        model = Company
        fields = ('comp_name', 'comp_address', 'comp_email', 'comp_region',  'comp_zip', 'comp_phone',
                  'comp_description', 'comp_city', 'comp_country', 'comp_website_url', 'comp_twitter_url',
                  'comp_fcbk_url')


class CustomCandidateForm(RegistrationForm):
    user_class_model = forms.CharField(initial="Candidate", label="user_class_model")
    first_name = forms.CharField()
    last_name = forms.CharField()
    candidate_region = forms.CharField()
    candidate_city = forms.CharField()
    candidate_birthday = forms.DateField()
    candidate_phone = forms.CharField()
    candidate_website_url = forms.URLField(required=False)
    candidate_github_url = forms.URLField(required=False)

    def __init__(self, *args, **kwargs):
        super(CustomCandidateForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.form_class = 'form-horizontal'
        self.helper.form_action = ''
        self.helper.label_class = 'col-sm-2'
        self.helper.field_class = 'col-sm-4'
        self.helper.layout = Layout(
            Fieldset(
                'Account details',
                Field('user_class_model'),
                Field('username', css_class='input-sm'),
                Field('email', css_class='input-sm'),
                Field('password1'),
                Field('password2'),
            ),
            Fieldset(
                'Personnal Informations ',
                Field('first_name', css_class='input-sm'),
                Field('last_name', css_class='input-sm'),
                Field('candidate_region', label='Region'),
                Field('candidate_city', label='City'),
                Field('candidate_birthday'),
                Field('candidate_phone'),
                Field('candidate_website_url'),
                Field('candidate_github_url'),
            ),

        )
        self.helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))


class CustomCompanyForm(RegistrationForm):
    user_class_model = forms.CharField(initial="Company", label="user_class_model")


class RegistrationViewCompany(RegistrationView):
    form_class = CustomCompanyForm


class RegistrationViewCandidate(RegistrationView):
    form_class = CustomCandidateForm

CustomCandidateForm.base_fields.update(CandidateForm.base_fields)
CustomCompanyForm.base_fields.update(CompanyForm.base_fields)

如您所见,我扩展了RegistrationView 和RegistrationForm。我的问题出在这条线上:

self.helper.form_action = ''

使用 form_action 的此参数,如果我的表单中有错误,我将在表单上重定向,并引发有关错误字段的错误。完美,但如果我的表单中没有错误(如果我的表单是干净的),django-registration 会引发此错误:

NotImplementedError at /user/accounts/register/candidate

No exception supplied

Request Method:     POST
Request URL:    http://127.0.0.1:8000/user/accounts/register/candidate
Django Version:     1.5.4
Exception Type:     NotImplementedError
Exception Location:     /home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/registration/views.py in register, line 109
Python Executable:  /home/damien/Developpements/Django/virtualenv/bin/python2.7
Python Version:     2.7.4
Python Path:    

['/home/damien/Developpements/Django/virtualenv/site_web/linuxemploi',
 '/home/damien/Developpements/Django/virtualenv/site_web/linuxemploi',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7/lib-tk',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7/lib-old',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages']

Server time:    sam, 2 Nov 2013 14:47:26 +0100

Traceback:
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/registration/views.py" in dispatch
  79.         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/registration/views.py" in post
  35.             return self.form_valid(request, form)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/registration/views.py" in form_valid
  82.         new_user = self.register(request, **form.cleaned_data)
File "/home/damien/Developpements/Django/virtualenv/lib/python2.7/site-packages/registration/views.py" in register
  109.         raise NotImplementedError

Exception Type: NotImplementedError at /user/accounts/register/candidate
Exception Value: 

如果我说:

 self.helper.form_action = '.'

我没有由 django-registration 引发错误,但是如果我的表单中有错误(表单不干净),我将重定向到/user/accounts/register而不是 /user/accounts/register* /candidate * 或/公司

有任何想法吗 ?

非常感谢

4

1 回答 1

2

从异常中可以直接看出,您必须register(request, **form.cleaned_data)在类中实现方法RegistrationViewCandidate。此方法必须实现您的自定义注册逻辑,因此未在 django-registration RegistrationView中实现

可能,您想修改现有的实现,比如从django-registration simple后端。那么你应该从你所做的继承RegistratioViewregistration.backends.simple.views不是继承。registration.views

于 2013-11-02T16:57:19.587 回答