この問題について数日後、私はあなたの助けが必要です!
django-registration フォームを拡張するには、2 つの異なる登録フォームが必要です。
私の 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')),
)
私のviews.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* /candidate * または/ではなく、/user/ accounts/registerにリダイレクトされます。会社。
何か案は ?
どうもありがとう