0

网址.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>

**

保存全名时:|____| 输入一个有效值。

**

4

2 回答 2

0

我认为你有一个更大的问题,那就是你假设一个人有一个名字和一个姓氏,用空格分隔。像 Anne Marie 这样的名字中有一个空格。为什么不直接询问他们的名字和姓氏?这在 Web 表单上是相当标准的。

于 2010-01-17T03:23:36.243 回答
0

问题是您的表单缺少 first_name 和 last_name 字段,因此在保存时甚至不会查找这些值。您可以在保存表单后手动填写这些值,但只有在表单验证后才能执行此操作 - 至少在 Firefox 正则表达式测试工具中,您的正则表达式在“Anne Marie”上失败 - 尝试将其更改为^[\w ]+$ 表单验证后,您可以先保存名字和姓氏是这样的:

if form.is_valid():
    new_user = form.save(profile_callback=profile_callback)
    new_user.first_name = form.cleaned_data['first_name']
    new_user.last_name = form.cleaned_data['last_name']
    new_user.save()

这段代码当然应该放在表单的保存方法中,但是对于快速测试来说没关系。

于 2010-01-17T08:14:55.770 回答