开发者

Full name while registration

urls.py

   url(r'^accounts/register/$',register,

{'form_class':RegForm},name='registration_register'),

form.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'''

views.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)

registration_form.html

 <dl class="vertical">
 &开发者_运维百科lt;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>

**

While saving Full Name: |_____| Enter a valid value.

**


I think you have a bigger problem, and that is that you're assuming that a person has one first name and one last name, separated by a space. First names such as Anne Marie have a space in them. Why not just ask them for a first name and last name? That's pretty standard on web forms.


The problem is that your form is missing first_name and last_name fields, so while saving it doesn't even look for these values. You can fill those values manually after saving the form, but you can do that only if form validates- at least in Firefox regexp testing tool your regex fails on "Anne Marie"- try changing it to ^[\w ]+$ After your form validates, you can save first name and last name like this:

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()

This code of course should go in form's save method, but for a fast test it's ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜