Introduction
This is an article for solving something which is not working when trying to add a login feature in the Django-based application. Basically, the fault exist in the declaration of the login form. The following is the description about the Django application :
-
The first one is the file containing the URL login definition in ‘urls.py’ file. The following is the content of the file which is pointing out the URL address definition for informing the login process’ function.
from django.urls import path from . import views urlpatterns = [ path('login/', views.loginPage, name="login"), ]
-
So, another important part is the ‘views.py’ which is containing the loginPage function. The following is the content of the ‘views.py’, specifically in the loginPage functio as follows :
def loginPage(request): page = 'login' if request.user.is_authenticated: return redirect('home') logging.basicConfig(level=logging.WARNING) logger = logging.getLogger('sysapp') if request.method == 'POST': username = request.POST.get('username').lower() password = request.POST.get('password') logging.info(username) print("Username : %s",username) logging.info(password) print("Password : %s",password) try: user = User.objects.get(username=username) except: messages.error(request, 'User does not exist') user = authenticate(request, username=username, password=password) logging.info("User : %s",user) if user is not None: print("login success") logging.info("logging in success") login(request, user) return redirect('home') else: messages.error(request, 'Username OR password does not exist') context = {'page':page} return render(request,'base/login_register.html', context)
-
The next one, it is obviously to check from the Django-based template file with the name of ‘login_register.html’. This follow is the content of the ‘login_register.html’ file :
{% extends 'main.html' %} {% block content %} {% if page == 'login' %} <div> <form method="POST" action=""> {% csrf_token %} <label>Username : </label> <input type="text" value="username" placeholder="Enter Username"/> <label>Password : </label> <input type="password" value="password" placeholder="Enter Password"/> <input type="submit" value="Login" /> </form> <p>Haven't signed up yet ?</p> <a href={% url 'register' %}>Sign up</a> </div> {% else %} <div> <form method="POST" action=""> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Register" /> </form> <p>Already signed up ?</p> <a href={% url 'login' %}>Login</a> </div> {% endif %} {% endblock %}
Solution
Although all of the scripts seem to be fine. There is actually an error in the Django-based template file. Instead of defining the variable name, it is mistook the declaration with the value name. Just look at the following snippet code of the ‘login_register.html’ file :
<form method="POST" action=""> {% csrf_token %} <label>Username : </label> <input type="text" value="username" placeholder="Enter Username"/> <label>Password : </label> <input type="password" value="password" placeholder="Enter Password"/> <input type="submit" value="Login" /> </form>
In other words, it is not supposed to be the value attribute or property. It should have been the name attribute or property. So, revise the form definition of the Django-based template file by changing the value attribute into a name attribute or just by simply adding the name attribute as follows :
<form method="POST" action=""> {% csrf_token %} <label>Username : </label> <input type="text" value="username" name="username" placeholder="Enter Username"/> <label>Password : </label> <input type="password" value="password" name="password" placeholder="Enter Password"/> <input type="submit" value="Login" /> </form>
The previous script cannot process the login form. It is because there are no attribute or property with the name of ‘username’ and ‘password’. Therefore, the loginPage function cannot get the value username and also the value password from the username attribute or property and the password attribute or property. By defining it in the form, it will give the input type element the identifier to define the username and the password.