开发者

Registration/authorization form with class-based generic views in django

C开发者_Go百科ould someone please help with reg/auth/auth using class-based generic views? It is clear how to do this with function-based views, but not with classes. Cannot understand the philosophy of CBV when rendering forms.


Why should it be necessarily Class-based if it works well with functional view ?

For registration I used this :

class SignUpFormView(FormView):
    form_class = SignUpForm
    template_name  = 'authentication/registration_form.html'

    def form_valid(self, form):
        """ What to do with the valid form ?
              - Create the user and a default website
              - Send a mail to confirm the email and the account creation

        """

        user = User.objects.create_user(form.cleaned_data['username'], 
                                        form.cleaned_data['email'], 
                                        form.cleaned_data['password'])
        user.is_active = False
        user.save()

        date = user.date_joined.replace(microsecond=0)
        key = hashlib.sha1((u'%s%s%s' % (settings.SECRET_KEY, user.email, date)
                            ).encode('utf-8')).hexdigest()

        subject = _(u'[%s] : Subscription') % settings.SITE_NAME

        mail = render_to_string('authentication/mails/registration_confirmation.html', 
                                { 'titre': subject,
                                  'pseudo': user.username,
                                  'site': settings.SITE_NAME,
                                  'user_id': user.id,
                                  'user_key': key })

        msg = EmailMessage(subject, mail, '%(site)s <%(email)s>' % {
                'site': settings.SITE_NAME, 'email': settings.DEFAULT_FROM_EMAIL
                }, [user.email])

        msg.content_subtype = "html"  # Main content is now text/html
        try:
            msg.send()
        except:
            # In debug we display the url
            print reverse('auth_activation', args=[user.id, key])

        return render_to_response("authentication/check_your_mail.html",
                                  context_instance=RequestContext(self.request))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜