I am building a website in django. I used login_required decorator to prevent user to move to UserView without a successful login. But I came to problem where its not moving from login page even after a successful login authentication and stays on the login page. What am I doing wrong?
Here is my code:
view.py
def LoginView(request):
if request.method=='POST':
username=request.POST.get('username')
password=request.POST.get('password')
try:
user_info=User_Registration.objects.get(username=username)
if user_info.password==password:
return redirect("userview")
else:
messages.info(request,"Invalid Password")
except User_Registration.DoesNotExist:
messages.info(request,"User Doesn't exist")
else:
return render(request,'login.html')
@login_required(login_url="login_view")
def UserView(request):
return render(request,'user.html')
urls.py
from django.urls import path
from post_app import views
urlpatterns=[
# path('',views.MainPageView.as_view()),
path('/login',views.LoginView,name='login_view'),
path('/userview',views.UserView,name='userview'),
path('/registration',views.RegistrationView,name='registration_view'),
path('new_job/<str:id>',views.post_new_job,name='new_job')
]
login.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="{% static 'login.css' %}">
<!------ Include the above in your HEAD tag ---------->
<body>
<div id="login">
<h3 class="text-center text-white pt-5">Login form</h3>
<div class="container">
<div id="login-row" class="row justify-content-center align-items-center">
<div id="login-column" class="col-md-6">
<div id="login-box" class="col-md-12">
<form id="login-form" class="form" action="" method="POST">
{% csrf_token %}
<h3 class="text-center text-info">Login</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input type="password" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-info btn-md" value="submit">
</div>
<div id="register-link" class="text-right">
<a href="{% url 'registration_view' %}" class="text-info">Register here</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div>
{% for message in messages %}
<br>
<h2 style="padding-left: 500px;" >{{message}}</h2>
{% endfor %}
</div>
</body>
</html>