New in django. Learning how to create a login page with user Authentication. All is working, but when I put wrong password for checking that the loop is working properly or not. Know that the error is due to wrong assignment of url, but can't understand how to solve it.
I am using two apps one for login(name=trevalo_app) and another for all (name=MYapp)
trevalo_app/views.py
from django.shortcuts import render,redirect
from django.http import *
from django.contrib.auth import authenticate,login,logout
from django.contrib import messages
from .models import *
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')
else:
messages.success(request,('There was an error in logining. Please Try again...'))
return redirect('login')
else:
return render(request,'login_user.html',{})
MYapp/index.html
<body>
{% include 'MYapp/navebar.html' %}
<center>
<h1>{{name}}</h1>
</center>
<div class="cotainer">
{% if messages %}
{% for message in messages %}
{{message}}
{% endfor %}
{% endif %}
</div>
{% block content %}{% endblock content %}
{% include 'MYapp/footer.html' %}
</body>
trevalo_app/urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path('login_user', views.login_user,name='login_user'),
]
MYapp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('footer', views.footer, name='footer'),
path('navebar', views.navebar, name='navebar'),
]
trevalo_app/login_user.html
{% extends 'MYapp/index.html' %}
{% block content %}
<center>
<h3>login...</h3>
<hr>
</center>
<div class="container container-fluid ">
<form method=POST action="">
{% csrf_token %}
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label"> username</label>
<input type="text" name='username' class="form-control"
placeholder="username" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" name='password' class="form-control" placeholder="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
{% endblock %}