2010-02-19 58 views
-4

这是注册视图:django错误,当我注册我的网站。为什么?

def signup(request, form_class=SignupForm, 
     template_name="account/signup.html", success_url=None): 
    if success_url is None: 
     success_url = get_default_redirect(request) 
    if request.method == "POST": 
     form = form_class(request.POST) 
     if form.is_valid(): 
      username, password = form.save() 
      if settings.ACCOUNT_EMAIL_VERIFICATION: 
       return render_to_response("account/verification_sent.html", { 
        "email": form.cleaned_data["email"], 
       }, context_instance=RequestContext(request)) 
      else: 
       user = authenticate(username=username, password=password) 
       auth_login(request, user) 
       request.user.message_set.create(
        message=_("Successfully logged in as %(username)s.") % { 
        'username': user.username 
       }) 
       return HttpResponseRedirect(success_url) 
    else: 
     form = form_class() 
    return render_to_response(template_name, { 
     "form": form, 
    }, context_instance=RequestContext(request)) 

和SignupForm:

class SignupForm(forms.Form): 

    username = forms.CharField(label=_("Username"), max_length=30, widget=forms.TextInput()) 
    password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False)) 
    password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput(render_value=False)) 

    if settings.ACCOUNT_REQUIRED_EMAIL or settings.ACCOUNT_EMAIL_VERIFICATION: 
     email = forms.EmailField(
      label = _("Email"), 
      required = True, 
      widget = forms.TextInput() 
     ) 
    else: 
     email = forms.EmailField(
      label = _("Email (optional)"), 
      required = False, 
      widget = forms.TextInput() 
     ) 

    confirmation_key = forms.CharField(max_length=40, required=False, widget=forms.HiddenInput()) 

    def clean_username(self): 
     if not alnum_re.search(self.cleaned_data["username"]): 
      raise forms.ValidationError(_("Usernames can only contain letters, numbers and underscores.")) 
     try: 
      user = User.objects.get(username__iexact=self.cleaned_data["username"]) 
     except User.DoesNotExist: 
      return self.cleaned_data["username"] 
     raise forms.ValidationError(_("This username is already taken. Please choose another.")) 

    def clean(self): 
     if "password1" in self.cleaned_data and "password2" in self.cleaned_data: 
      if self.cleaned_data["password1"] != self.cleaned_data["password2"]: 
       raise forms.ValidationError(_("You must type the same password each time.")) 
     return self.cleaned_data 

    def save(self): 
     username = self.cleaned_data["username"] 
     email = self.cleaned_data["email"] 
     password = self.cleaned_data["password1"] 

     if self.cleaned_data["confirmation_key"]: 
      from friends.models import JoinInvitation # @@@ temporary fix for issue 93 
      try: 
       join_invitation = JoinInvitation.objects.get(confirmation_key = self.cleaned_data["confirmation_key"]) 
       confirmed = True 
      except JoinInvitation.DoesNotExist: 
       confirmed = False 
     else: 
      confirmed = False 

     # @@@ clean up some of the repetition below -- DRY! 

     if confirmed: 
      if email == join_invitation.contact.email: 
       new_user = User.objects.create_user(username, email, password) 
       join_invitation.accept(new_user) # should go before creation of EmailAddress below 
       new_user.message_set.create(message=ugettext(u"Your email address has already been verified")) 
       # already verified so can just create 
       EmailAddress(user=new_user, email=email, verified=True, primary=True).save() 
      else: 
       new_user = User.objects.create_user(username, "", password) 
       join_invitation.accept(new_user) # should go before creation of EmailAddress below 
       if email: 
        new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email}) 
        EmailAddress.objects.add_email(new_user, email) 
     else: 
      new_user = User.objects.create_user(username, "", password) 
      if email: 
       new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email}) 
       EmailAddress.objects.add_email(new_user, email) 

     if settings.ACCOUNT_EMAIL_VERIFICATION: 
      new_user.is_active = False 
      new_user.save() 

     return username, password # required for authenticate() 
+0

那么你的SignupForm样子? – buckley 2010-02-19 02:15:08

+2

-1:哇,这很令人困惑。这是很多代码。问题是什么? – 2010-02-19 03:32:38

+0

至少附加异常堆栈跟踪或错误,以便对别人有帮助? – tutuDajuju 2013-05-20 10:49:54

回答

0

现在是确定

我使用的MySQL与其使用squite3

相关问题