title: Django log user in using class based view
body: <p>I am trying to log a user in, however, my view does not seem to work. The page is rendered and the form is displayed and everything. But when I enter a valid user name and password it just refreshes the page instead of going to <code>success_url</code>. I think it is the way I am implementing my view method. I do not think <code>login(request, user_obj)</code> is being accessed, and if so, so is <code>request.user.is_authenticated()</code>.</p>
  
  <p>I also include my login form using </p>
  
  <pre><code>{% if user.is_authenticated %}
      {% include 'navbar_in.html' %}
  {% else %}
      {% include 'navbar_out.html' %}
  {% endif %}
  </code></pre>
  
  <p>It only uses <code>navbar_out.html</code>so that is how I know my code is not accessing <code>login(request, user_obj)</code>.</p>
  
  <p>User model:</p>
  
  <pre><code># Create your models here.
  class Usermie(models.Model):
      # associate fields with django built in user objects
      # this provides the authentication that we would need from the django built in utilities
      usermie_object = models.OneToOneField(User)
  
      # form fields
      id = models.AutoField(primary_key=True)
      username = models.CharField(max_length=30, unique=True, blank=False)
      email = models.EmailField(max_length=30, unique=True, blank=False, null=False)
      first_name = models.CharField(max_length=30, blank=False)
      last_name = models.CharField(max_length=30, blank=False)
      birthday = models.DateField(blank=False)
      password = models.CharField(max_length=50, null=False)
      sex = models.CharField(max_length=1, blank=False)
      location = models.CharField(max_length=100, null=True)
      city = models.CharField(max_length=40, null=True)
      province = models.CharField(max_length=40, null=True)
      country = models.CharField(max_length=40, null=True)
      activated = models.IntegerField()  # will be 1 if account is activated, 0 otherwise.
      date_created = models.DateTimeField(default=timezone.now)  # the time the account is created
      date_activated = models.DateTimeField(null=True)  # when the account is activated via email
  
      def __str__(self):
          return self.username
  
  
  # create a user object to attach to our forms (SignUp)object
  def create_usermie_user_callback(sender, instance, **kwargs):
      usermie, new = Usermie.objects.get_or_create(usermie_object=instance)
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>from .models import Usermie
  from django.shortcuts import render
  from django.views.generic import View
  from .forms import SignUpForm, LoginForm
  from django.contrib.auth.models import User
  from django.http import HttpResponseRedirect
  from django.views.generic.base import TemplateView
  from django.contrib.auth import authenticate, login, logout
  
  class UserLoginRegistration(View):
      form_class = LoginForm
  
      # Use initial to declare the initial value of form fields at runtime.
      # For example, you might want to fill in a username field with
      # the username of the current session.
      initial = {'key': 'value'}
      template_name = 'usermie/usertest.html'  # template form will be rendered on
      success_url = '/usermie/home/'  # template for successfully submitted form
  
      def get(self, request, *args, **kwargs):
  
          form_login = self.form_class(initial=self.initial)
  
          return render(request, self.template_name, {'form_login': form_login})
  
      # method for posting form
      def post(self, request, *args, **kwargs):
  
          # Accessing form with post data
          form_login = self.form_class(request.POST)
  
          # Checking if user is logged in
          if request.user.is_authenticated():
              # Making sure user does not log in twice,
              # just send user to profile.html if already logged in
              return HttpResponseRedirect(self.success_url)
  
          # Checking if the form is valid
          if form_login.is_valid():
              email = form_login.cleaned_data['email']
              password = form_login.cleaned_data['password']
  
              # NB! On Django docs Two methods to authenticate user and log them in
              # 1 call authenticate
              # 2 call login
  
              # Return a user_obj object if the username and password are valid
              # otherwise it will return null, the null variable is called None in python
              user_obj = authenticate(email=email, password=password)
              if user_obj is not None:
                  if user_obj.is_active:
                      login(request, user_obj)
                      return HttpResponseRedirect(self.success_url)
  
              # If authentication failed
              else:
                  return HttpResponseRedirect(self.template_name)
          # If form is not being posted, render form to template
          else:
              form_login = self.form_class(initial=self.initial)
              context = {'form_login': form_login}
              return render(request, self.template_name, context)
  </code></pre>
  
  <p>And this is my mark up</p>
  
  <pre><code>&lt;form class="navbar-form navbar-form-out" action="" method="post"&gt;
      {% csrf_token %}
      {% load widget_tweaks %}
      &lt;div class="form-group"&gt;
          &lt;label class="sr-only" for="{{ form_login.email.auto_id }}"&gt;{{ form_login.email.label }}&lt;/label&gt;
          {% render_field form_login.email class="form-control" placeholder=form_login.email.label %}
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label class="sr-only" for="{{ form_login.auto_id }}"&gt;{{ form_login.password.label }}&lt;/label&gt;
          {% render_field form_login.password class="form-control" placeholder=form_login.password.label %}
          {% for hidden in form_login.hidden_fields %}
              {{ hidden }}
          {% endfor %}
      &lt;/div&gt;
      &lt;div class="checkbox"&gt;
          &lt;label&gt;
              &lt;input type="checkbox"&gt; Remember me
          &lt;/label&gt;
      &lt;/div&gt;
      &lt;button type="submit" name="" value="form_login" class="btn btn-default"&gt;Sign in&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>