# Authentication Form not working properly.

**URL:** <https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849>\
**Category:** Forms & APIs\
**Created:** [June 25, 2023, 2:51pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849 "2023-06-25T14:51:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UsamaHussain8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/usamahussain8/32/12774_2.png) [@UsamaHussain8](https://forum.djangoproject.com/u/UsamaHussain8)\
**Post date:** [June 25, 2023, 2:51pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/1 "2023-06-25T14:51:43Z")

</div>

Hello.

I am trying to create a very basic login system (just to grasp Django’s authentication system, nothing fancy).

Thing is, when I use the following code (i.e. don’t validate authentication form submission), everything **works perfectly**.

The _views.py_ file:

```python
from django.shortcuts import render, redirect
from django.http import request
from django.contrib.auth.decorators import login_required
from .forms import UserRegistrationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm

@login_required(login_url="accounts/login")
def home(request):
    return render(request, "main/home.html")

def register_user(request):
    if request.method == "POST":
        reg_form = UserRegistrationForm(request.POST)
        if reg_form.is_valid():
            user = reg_form.save()

            messages.success(request, f'You have successfully registered!')
            return redirect("login")
        else:
            messages.error(request, "Some problem occured")
    else:
        reg_form = UserRegistrationForm()
    return render(request, template_name='main/register.html', context={"form":reg_form})
    
def login_view(request):
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = authenticate(request, username = username, password = password)

        if user is not None:
            login(request, user)
            messages.success(request, f"You logged in as: {username}")
                    
            return redirect("home")
        else:
            messages.error(request, "Invalid username or password")
            return redirect("login")
    else:
        auth_form = AuthenticationForm()
    return render(request, "registration/login.html", context={"form":auth_form})

def logout_request(request):
    logout(request)
    messages.success(request, f"You have logged out.")
    return redirect("login")

```

However, when I insert authentication form validation and population, user is not authenticated and None is returned instead.

For the purpose of simplicity I am adding the ‘login\_view’ only as this is where I encounter the problem.

```python
def login_view(request):
    if request.method == "POST":
        auth_form = AuthenticationForm(request.POST)
        if auth_form.is_valid():
            username = auth_form.cleaned_data.get("username")
            password = auth_form.cleaned_data.get("password")
            user = authenticate(request, username = username, password = password)

            if user is not None:
                login(request, user)
                messages.success(request, f"You logged in as: {username}")
                    
                return redirect("home")
            else:
                messages.error(request, "Invalid username or password")
                return redirect("login")
        else:
            messages.error(request, f"Some problem encountered.")
            return redirect("login")
    else:
        auth_form = AuthenticationForm()
    return render(request, "registration/login.html", context={"form":auth_form})

```

**NOTE** : I know such an if-else ladder is discouraged and shouldn’t be used. But I’m just curious as to why the former solution works and the latter one does not.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2023, 4:38pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/2 "2023-06-25T16:38:36Z")

</div>

It will help you diagnose the errors you are encountering if you print (or render) the errors reported by the form from the `is_valid` test. See the [Form.errors](https://docs.djangoproject.com/en/4.2/ref/forms/api/#django.forms.Form.errors) docs.

---

<div class="post-metadata">

**Author:** ![UsamaHussain8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/usamahussain8/32/12774_2.png) [@UsamaHussain8](https://forum.djangoproject.com/u/UsamaHussain8)\
**Post date:** [June 25, 2023, 6:36pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/3 "2023-06-25T18:36:27Z")

</div>

Ok. So, I entered the print statement (using auth\_form.errors.get\_data() ) in both the ‘is\_valid’ section and the corresponding else statement. It just gives me ‘{}’.

Also, when I insert a print statement in the is\_valid section to print the entered username, nothing is printed. And when I add this statement in the corresponding else, the correct username gets printed.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2023, 6:46pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/4 "2023-06-25T18:46:50Z")

</div>

Please post the updated code where you’re trying all this. (I don’t see a `get_data` method, I see an `as_data` and a `get_json_data`, so I would actually expect you to get an error message if that’s what you tried.)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2023, 6:51pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/5 "2023-06-25T18:51:12Z")

</div>

Also note that the `clean` method of the django.contrib.auth.forms.AuthenticationForm performs the `authenticate` call - that’s not something you need to do within your view. It also raises its own error messages internally.

I suggest you review how that form works - you’re doing a lot of work here that is trying to replicate what that form already provides.

---

<div class="post-metadata">

**Author:** ![UsamaHussain8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/usamahussain8/32/12774_2.png) [@UsamaHussain8](https://forum.djangoproject.com/u/UsamaHussain8)\
**Post date:** [June 26, 2023, 5:30pm UTC](https://forum.djangoproject.com/t/authentication-form-not-working-properly/21849/6 "2023-06-26T17:30:37Z")

</div>

Yes, I mistook its working to be the same as other Model Forms.

It provides its own authentication mechanism and generates its own errors.

Also, it takes a request parameter (mandatory) and after that we can pass request.POST as data (using kwargs).

Thanks loads for the help 🙂
