0
votes

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 success_url. I think it is the way I am implementing my view method. I do not think login(request, user_obj) is being accessed, and if so, so is request.user.is_authenticated().

I also include my login form using

{% if user.is_authenticated %}
    {% include 'navbar_in.html' %}
{% else %}
    {% include 'navbar_out.html' %}
{% endif %}

It only uses navbar_out.htmlso that is how I know my code is not accessing login(request, user_obj).

User model:

# 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)

View:

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)

And this is my mark up

<form class="navbar-form navbar-form-out" action="" method="post">
    {% csrf_token %}
    {% load widget_tweaks %}
    <div class="form-group">
        <label class="sr-only" for="{{ form_login.email.auto_id }}">{{ form_login.email.label }}</label>
        {% render_field form_login.email class="form-control" placeholder=form_login.email.label %}
    </div>
    <div class="form-group">
        <label class="sr-only" for="{{ form_login.auto_id }}">{{ form_login.password.label }}</label>
        {% render_field form_login.password class="form-control" placeholder=form_login.password.label %}
        {% for hidden in form_login.hidden_fields %}
            {{ hidden }}
        {% endfor %}
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox"> Remember me
        </label>
    </div>
    <button type="submit" name="" value="form_login" class="btn btn-default">Sign in</button>
</form>
1
For the default user model, authenticate expects username and password, not email and password. You haven't shown your user model, so we can't tell whether email and password will work for you. - Alasdair
Let me check this out with model and I will get back with you. - Jam1
I changed email to username in my forms and nothing works for the user I have created, however when I login with the admin username and password it works right (not talking about logging in through the admin page). I do not know for what reason this is happening. - Jam1
You are storing the password in plain text. You must not do this, it's insecure. The Django docs explain how to create a custom user model properly. - Alasdair
The authentication backends used by the login method are designed to work with the auth.User model (or your custom user model if you have set AUTH_USER_MODEL. I can't help you make it work for a different model. - Alasdair

1 Answers

0
votes

It might be that your user is not active and you don't handle that case. Try something like:

if user_obj.is_active:
    login(request, user_obj)
    return HttpResponseRedirect(self.success_url)
else:
    return HttpResponse("Your account is inactive.")