title: Django CBV Authentication login/logout solution
body: <p>I got the CBV setup to login from going to URL "/" of my Django website, but I was trying to get the login to redirect to the URL "/blah/" after successful login.</p>
  
  <p>This is my myapp/urls.py</p>
  
  <pre><code>from django.conf.urls import include, url
  from django.contrib import admin
  from content import views as content_views
  
  urlpatterns = [
      url(r'^', include('content.urls')),
      url(r'^(?P&lt;slug&gt;[a-zA-z0-9]+)/$', content_views.BlahDetailView.as_view(), name='blah_detail'),]
  </code></pre>
  
  <p>Here is my content/urls.py</p>
  
  <pre><code>from django.conf.urls import url    
  from . import views
  
  app_name = 'content'
  urlpatterns = [
      url(r'^$', views.IndexList.as_view(), name='index'),
      url(r'^login/$', views.LoginView.as_view(), name='login'),
      url(r'^logout/$', views.LogoutView.as_view(), name='logout'),
  ]
  </code></pre>
  
  <p>Here is content/views.py</p>
  
  <pre><code>class LoginView(FormView):
      """
      Provides the ability to login as a user with a username and password
      """
      success_url = '/'
      form_class = AuthenticationForm
      redirect_field_name = REDIRECT_FIELD_NAME
      template_name = 'content/login.html'
  
      @method_decorator(sensitive_post_parameters('password'))
      @method_decorator(csrf_protect)
      @method_decorator(never_cache)
      def dispatch(self, request, *args, **kwargs):
          # Sets a test cookie to make sure the user has cookies enabled
          request.session.set_test_cookie()
          return super(LoginView, self).dispatch(request, *args, **kwargs)
  
      def form_valid(self, form):
          auth_login(self.request, form.get_user())
          # If the test cookie worked, go ahead and
          # delete it since its no longer needed
          if self.request.session.test_cookie_worked():
              self.request.session.delete_test_cookie()
          return super(LoginView, self).form_valid(form)
  
      def get_success_url(self):
          import ipdb; ipdb.set_trace()
          redirect_to = self.request.GET.get(self.redirect_field_name)
          if not is_safe_url(url=redirect_to, host=self.request.get_host()):
              redirect_to = self.success_url
          return redirect_to
  
  
  class LogoutView(RedirectView):
      """
      Provides users the ability to logout
      """
      url = '/login/'
  
      def get(self, request, *args, **kwargs):
          import ipdb; ipdb.set_trace()
          if self.request.user.is_authenticated():
              auth_logout(request)
          return super(LogoutView, self).get(request, *args, **kwargs)
  
  class IndexList(generic.ListView):
      template_name = 'content/index.html'
      context_object_name = 'home_page'
      queryset = &lt;some object&gt;
  
      @method_decorator(login_required)
      def dispatch(self, *args, **kwargs):
          if self.request.user.is_active:
              return super(IndexList, self).dispatch(*args, **kwargs)
          else:
              return HttpResponseRedirect('%s?next=%s' % (settings.LOGIN_URL, self.request.path))
  
      def get_context_data(self, **kwargs):
          context = super(IndexList, self).get_context_data(**kwargs)
          if self.request.user.is_authenticated():
              context['authenticated_user'] = True
          else:
              context['authenticated_user'] = False
          &lt;&lt;some other context data here&gt;&gt;
          return context
  
  class BlahDetailView(generic.DetailView):
      model = Obj
      template_name = 'content/blah'
      context_object_name = 'blah_detail'
  
      @method_decorator(login_required)
      def dispatch(self, *args, **kwargs):
          if self.request.user.is_active:
              return super(BlahDetailView, self).dispatch(*args, **kwargs)
          else:
              return HttpResponseRedirect('?next=%s' % (self.request.path))
  
      def get_queryset(self):
          self.page = get_object_or_404(Page, slug=self.kwargs['slug'])
          return Obj.objects.filter(slug=self.kwargs['slug'], enabled=True).order_by('name')
  
      def get_context_data(self, **kwargs):
          context = super(BlahDetailView, self).get_context_data(**kwargs)
          if self.request.user.is_authenticated():
              context['authenticated_user'] = True
          else:
              context['authenticated_user'] = False
          &lt;&lt; some other context data here&gt;&gt;
          return context
  </code></pre>
  
  <p>And this is the content/login.html</p>
  
  <pre><code>&lt;form method="post" action="{% url 'content:login' %}"&gt; {% csrf_token %}
      &lt;input type="hidden" name="next" value="{{ next }}" /&gt;
      &lt;input type="text" name="username" placeholder="Username"/&gt;&lt;br/&gt;
      &lt;input type="password" name="password" placeholder="Password"/&gt;&lt;br/&gt;
  
      &lt;input type="submit" value="Login"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When I go to "/" it opens the login page, then I log in and it redirects to the "/". But when I go to "/blah/" it opens the login page, but it does not redirect to "/blah/". I see that I have success_url set to "/", I just don't understand how I can redirect it to "/blah/" or if I go to any other url. I am a newbie, so I apologize if I am not seeing something self evident. Thanks --simi</p>