1

有人可以使用基于类的通用视图来帮助使用reg/auth/auth吗?很明显,如何使用基于函数的视图来完成此操作,但不能使用类。渲染表单时无法理解CBV的理念。在django中使用基于类的通用视图的注册/授权表

+0

你能明确什么你到底想干什么? – Natim 2011-05-13 09:21:06

+0

我想使用我自己的基于类的视图来创建授权和注册表单。 – megido 2011-05-13 14:33:35

+0

所以基本上你想要两个班?一个用于注册另一个身份验证? – Natim 2011-05-16 10:11:41

回答

1

为什么它应该是基于类的,如果它与功能视图一起使用的很好?

对于注册我用这个:

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

谢谢!我会试试看! – megido 2011-05-17 11:58:27

+0

激活类怎么样? – un33k 2012-02-02 02:47:29

+0

你是什么意思? – Natim 2012-02-02 12:53:19

相关问题