2014-06-05 74 views
0

這是一個繼承自[django-registration][1]的自定義註冊表單。我的額外表單字段沒有出現,字段清理方法也沒有運行,這是我從功能上觀察到的,並且是由打印語句支持的(Custom form runs打印語句運行但不是'自定義密碼清理')。字段出現並且驗證運行當放置到原始django-registration代碼,提供如下。Django表單子類不起作用

這是爲什麼?

my app/forms.py 

from registration.forms import RegistrationForm 

class CustomRegistrationForm(RegistrationForm): 
""" 
Form for registering a new user account. 

Validates that the requested username is not already in use, and 
requires the password to be entered twice to catch typos. 

I have has added email uniqueness validation and minimum 
password length validation. 

Subclasses should feel free to add any additional validation they 
need, but should avoid defining a ``save()`` method -- the actual 
saving of collected user data is delegated to the active 
registration backend. 

""" 
print 'Custom form runs' 
LOCALITIES = (
    ('1', 'London'), 
    ('2', 'Berlin'),  
) 
locality = forms.MultipleChoiceField(choices=LOCALITIES, 
    label='Where are you?', 
    widget=forms.CheckboxSelectMultiple) 

def clean_password1(self): 
    """ 
    Verify that password is longer than 5 characters. 

    """ 
    print 'Custom Password clean' 
    password = self.cleaned_data['password1'] 
    print 'custom valid' 
    if len(password) < 6: 
     raise forms.ValidationError(_("Password needs to be at least 6 characters long")) 
    return password 

def clean_email(self): 
    """ 
    Validate that the supplied email address is unique for the site. 

    """ 
    email = self.cleaned_data['email'] 
    if User.objects.filter(email__iexact=self.cleaned_data['email']): 
     raise forms.ValidationError(_("This email address is already in use. \ 
      Please supply a different email address.")) 
    return email 

django的登記

registration/forms.py

""" 
Forms and validation code for user registration. 

Note that all of these forms assume Django's bundle default ``User`` 
model; since it's not possible for a form to anticipate in advance the 
needs of custom user models, you will need to write your own forms if 
you're using a custom model. 

""" 

from django.contrib.auth.models import User 
from django import forms 
from django.utils.translation import ugettext_lazy as _ 

class RegistrationForm(forms.Form): 
""" 
Form for registering a new user account. 

Validates that the requested username is not already in use, and 
requires the password to be entered twice to catch typos. 

Subclasses should feel free to add any additional validation they 
need, but should avoid defining a ``save()`` method -- the actual 
saving of collected user data is delegated to the active 
registration backend. 

""" 
required_css_class = 'required' 

username = forms.RegexField(regex=r'^[\[email protected]+-]+$', 
          max_length=30, 
          label=_("Username"), 
          error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")}) 
email = forms.EmailField(label=_("E-mail")) 
password1 = forms.CharField(widget=forms.PasswordInput, 
          label=_("Password")) 
password2 = forms.CharField(widget=forms.PasswordInput, 
          label=_("Password (again)")) 



def clean_username(self): 
    """ 
    Validate that the username is alphanumeric and is not already 
    in use. 

    """ 
    existing = User.objects.filter(username__iexact=self.cleaned_data['username']) 
    if existing.exists(): 
     raise forms.ValidationError(_("A user with that username already exists.")) 
    else: 
     return self.cleaned_data['username'] 


def clean_email(self): 
    """ 
    Validate that the supplied email address is unique for the site. 

    """ 
    email = self.cleaned_data['email'] 
    if User.objects.filter(email__iexact=self.cleaned_data['email']): 
     raise forms.ValidationError(_("This email address is already in use. \ 
      Please supply a different email address.")) 
    return email 

def clean(self): 
    """ 
    Verify that the values entered into the two password fields 
    match. Note that an error here will end up in 
    ``non_field_errors()`` because it doesn't apply to a single 
    field. 

    """ 
    if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: 
     if self.cleaned_data['password1'] != self.cleaned_data['password2']: 
      raise forms.ValidationError(_("The two password fields didn't match.")) 
    return self.cleaned_data 
+0

「自定義窗體運行」將打印時表單首先被導入。但是你在哪裏使用表單? –

+0

我用django註冊的形式。因此,在'urls.py'中:'from crewcal.forms import'CustomRegistrationForm url(r'^ register/$','RegistrationView', {'form_class':CustomRegistrationForm, 'backend':'registration.backends.default。 DefaultBackend'},name ='registration_register'),' – KindOfGuy

回答

1

這不是你如何定製基於類的視圖。 urlconf中的第三個參數是作爲請求的一部分傳遞的參數,以及從URL本身捕獲的參數:它不用於配置類視圖。

爲了做到這一點,您應該覆蓋在你的代碼的類,並設置屬性那裏,或者你可以將它們作爲參數傳遞給類的as_view()方法:

url(r'^register/$', RegistrationView.as_view(form_class=CustomRegistrationForm, backend=registration.backends.default.DefaultBackend), name='registration_register'),