Help me - Django and python

21 views
Skip to first unread message

avdesh sharma

unread,
Jun 22, 2021, 1:12:54 AM6/22/21
to django...@googlegroups.com
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.png




--
Warm Regards,
Avdesh Kumar Sharma
9650031844

DJANGO DEVELOPER

unread,
Jun 22, 2021, 2:25:40 AM6/22/21
to django...@googlegroups.com
can you please share your login view code?

--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF5Nfo65s-8c75FRkhF0rN7Sh0aUH6eKNBbQ--OMawNPCVX13w%40mail.gmail.com.

DJANGO DEVELOPER

unread,
Jun 22, 2021, 2:26:04 AM6/22/21
to django...@googlegroups.com
I think you're not returning anything in your model.

avdesh sharma

unread,
Jun 22, 2021, 3:53:08 AM6/22/21
to django...@googlegroups.com
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>

DJANGO DEVELOPER

unread,
Jun 22, 2021, 5:17:08 AM6/22/21
to django...@googlegroups.com
by seeing your code, it seems that only the 'else' part is working. have you provided value='{{dict_key.model_password_field}} in your html input element?

DJANGO DEVELOPER

unread,
Jun 22, 2021, 5:21:47 AM6/22/21
to django...@googlegroups.com
your 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 %}


changed 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" value="{{login.password_field}}">

</div>
<input type="hidden" value="student_tab" name="tab_selected">
{% if login == 'inactive' %}
<label for="error" style="color: red;">User Inactive! Contact Admin!</label>
{% else %}

<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 %}

avdesh sharma

unread,
Jun 22, 2021, 9:32:43 AM6/22/21
to django...@googlegroups.com
what will happen if I change from elif to else ?
FYI - I checked it but still getting same error. 

DJANGO DEVELOPER

unread,
Jun 22, 2021, 11:34:41 AM6/22/21
to django...@googlegroups.com
because it is necessary to write when there are no more conditions left.

DJANGO DEVELOPER

unread,
Jun 22, 2021, 11:35:18 AM6/22/21
to django...@googlegroups.com
have you removed for loop from your form?
Reply all
Reply to author
Forward
0 new messages