Here is my View.py code

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth import authenticate, logout, login
from django.contrib.auth.decorators import login_required
from student.models import *
from django.views.generic import TemplateView
import datetime, random
from django.contrib.auth.backends import BaseBackend
# Create your views here.


def index_view(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        tab_selected = request.POST.get('tab_selected')

        user = authenticate(username=username, password=password)
        print(user)
        if user:
        # if get_user(username):
            if user.is_active:
                login(request, user)
                request.session['username'] = username
                request.session['tab_selected'] = tab_selected
                if tab_selected == 'student_tab':
                    return HttpResponseRedirect(reverse('student:stud_home'))
                elif tab_selected == 'dept_tab':
                    if username[:3] == 'hod':
                        return HttpResponseRedirect(reverse('student:hod_home'))
                    else:
                        return HttpResponseRedirect(reverse('student:dep_home'))
                elif tab_selected == 'office_tab':
                    return HttpResponseRedirect(reverse('student:off_home'))
            else:
                user_login = 'inactive'
                return render(request, 'student/index.html',
context={'login': user_login})
        else:
            user_login = 'invalid'
            return render(request, 'student/index.html',
context={'login': user_login})
    else:
        return render(request, 'student/index.html', context={})


index.html

<div class="form-group">
    <label for="password" style="color:black;"><b>PASSWORD</b></label></br>
    <input type="password" style="width: 15rem; margin: auto;"
class="form-control" name="password" placeholder="Enter Password">
</div>
<input type="hidden" value="student_tab" name="tab_selected">
{% if login == 'inactive' %}
    <label for="error" style="color: red;">User Inactive! Contact Admin!</label>
{% elif login == 'invalid' %}
    <label for="error" style="color: red;">line 32 username or
password Invalid</label>
{% endif%}
{% if tab_error == True %}
    <label for="error" style="color: red;">Invalid Tab Selected</label>
{% endif %}
<br>


On Tue, Jun 22, 2021 at 11:55 AM DJANGO DEVELOPER <[email protected]>
wrote:

> can you please share your login view code?
>
> On Tue, Jun 22, 2021 at 10:12 AM avdesh sharma <[email protected]>
> wrote:
>
>> Hi All,
>>
>> I have an issue coming in my django code, even though the user id and
>> password is present in sqlite3 db still  the user is giving '*None'*
>> value
>> user = authenticate(username=username, password=password)
>>
>> and I am not able to login with user and throwing this error.
>>
>> [image: image.png]
>>
>>
>>
>>
>> --
>> Warm Regards,
>> Avdesh Kumar Sharma
>> 9650031844
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAF5Nfo65s-8c75FRkhF0rN7Sh0aUH6eKNBbQ--OMawNPCVX13w%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAF5Nfo65s-8c75FRkhF0rN7Sh0aUH6eKNBbQ--OMawNPCVX13w%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAKPY9p%3DSQDqv12b8K52FuG9mp40SM9sLFAQ4meaPM37NJxM4zQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAKPY9p%3DSQDqv12b8K52FuG9mp40SM9sLFAQ4meaPM37NJxM4zQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Warm Regards,
Avdesh Kumar Sharma
9650031844

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAF5Nfo7JkSetxb-kfGAjnRLJiCBG%2BvOEcgj38Oe0migcXEffvg%40mail.gmail.com.

Reply via email to