'Django login user never come true
I am having problems with my Django login.
The below if statement (if user is not None:) always resolves as false, so it moves onto the else.
I'm not sure why and I would appreciate any help
Thanks
VIEWS.PY
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib import messages
# Create your views here.
def login_user(request):
if request.method == 'post':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('index.html')
else:
messages.success(request,("there is an error...."))
return redirect('login')
else:
return render (request ,'login.html', {})
urls.py
from django.urls import path
from accounts import views
urlpatterns = [
path('login_user', views.login_user ,name= 'login'),
]
main urls.py
""
from django.contrib import admin
from django.urls import path,include
from availability import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('availability.urls'), name='index'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('accounts.urls')),
]
'''
And here is the login form:
<form action="" method="post">
{% csrf_token %}
<div class="input-group mb-3">
<input type="text" class="form-control" name='username' placeholder="username">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-user"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" class="form-control" name='password' placeholder="Password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-8">
<div class="icheck-primary">
<input type="checkbox" id="remember">
<label for="remember">
Remember Me
</label>
</div>
</div>
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Sign In</button>
</div>
</div>
</form>
I'm trying to create a custom login form. Below is what I have so far. My issues is that no matter what I do the login never authenticates. Even tho I have the correct email and password I always get:
Solution 1:[1]
As stated in the official documentation, the string representation of the request method is guaranteed to be uppercase.
Thus, you will have to change your if statement to
def login_user(request):
if request.method == 'POST': # Change here
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('index.html')
else:
messages.success(request,("there is an error...."))
return redirect('login')
else:
return render (request ,'login.html', {})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JSRB |
