网址.py
url(r'^accounts/register/$',register,
{'form_class':RegForm},name='registration_register'),
表格.py
from registration.forms import *
class RegForm(RegistrationForm):
"""
"""
fullname = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'fullname'))
def clean_fullname(self):
return self.cleaned_data['fullname']
def clean(self):
if not self.errors:
self.cleaned_data['first_name']='%s'(self.cleaned_data['fullname'].split('')[0])
self.cleaned_data['last_name'] = '%s'(self.cleaned_data['fullname'].split('')[1])
super(RegForm, self).clean()
return self.cleaned_data'''
视图.py
def register(request, success_url=None,
form_class=RegForm, profile_callback=None,
template_name='registration/registration_form.html',
extra_context=None):
#pform_class = utils.get_profile_form()
if request.method == 'POST':
#profileform = pform_class(data=request.POST,files=request.FILES)
form = form_class(data=request.POST,files=request.FILES)
if form.is_valid():
new_user = form.save(profile_callback=profile_callback)
#profile_obj = profileform.save(commit=False)
#profile_obj.user = new_user
#profile_obj.save()
return HttpResponseRedirect(success_url or reverse('registration_complete'))
else:
form = form_class()
#profileform = pform_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,{'form':form},context_instance=context)
注册表格.html
<dl class="vertical">
<dt><label class="required"for="fullname">Full Name</label></dt>
<dd>
<div class="formHelp"></div>
{{ form.fullname }}
{% for error in form.fullname.errors%}
<span style="color:red">{{ error }}</span>
{% endfor %}
</dd>
<dt><label class="required" for="username">User Name</label></dt>
<dd>
<div class="formHelp"></div>
{{ form.username }}
{% for error in form.username.errors%}
<span style="color:red">{{ error }}</span>
{% endfor %}
</dd>
<dt><label class="required" for="email">Email Address</label></dt>
<dd>
<div class="formHelp"></div>
{{ form.email }}
{% for error in form.email.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</dd>
<dt><label for="password"class="required">Password</label></dt>
<dd>
<div class="formHelp"></div>
{{ form.password1 }}
{% for error in form.password1.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</dd>
<dt><label for="password2"class="required">Confirm Password</label></dt>
<dd>
<div class="formHelp"></div>
{{ form.password2 }}
{% for error in form.password2.errors %}
<span style="color:red">{{ error }}</span>
{% endfor %}
</dd>
**
保存全名时:|____| 输入一个有效值。
**