title: Setting a condition based redirect in a class based view (Django)
body: <p>I'm (beginner) creating a simple blog app. I created a class based view to create a blog post and it has a separate URL. I want it redirect to the login page (url name = 'login') if the user isn't logged in. In a function based view all I have to do is this:</p>
  
  <pre><code>def postCreateView(request):
  if not request.user.is_authenticated:
      return redirect('login')
  </code></pre>
  
  <p>I don't know how to do this in a class based view (inherited from generic CreateView). I tried modifying the <strong>init</strong>() method like this:</p>
  
  <pre><code>def redirectLogin(self):
       return redirect('login')
  
   def __init__(self, *args, **kwargs):
       if(not self.request.user.is_authenticated):
           redirectLogin()
       super().__init__()
  </code></pre>
  
  <p>(I named the class PostCreateView) But on debugging the Exception Value was <code>'PostCreateView' object has no attribute 'request'</code>. I need the correct way (The Django convention way if it exists) to set a condition based redirect in class based view.</p>
  
  <p>Also I'm confused why it says the view doesn't have an attribute 'request'. Every view has request. I'm sure about it because I overrode the <code>form_valid()</code> method to set the author of the post as the currently logged in user like this:</p>
  
  <pre><code>def form_valid(self, form):
      form.instance.author = self.request.user
      return super().form_valid()
  </code></pre>
  
  <p>And it run perfectly fine when I logged in and created a post.</p>
  
  <p>Thanks in advance.</p>