Creating a login page with user Authentication & this happened : raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)

Viewed 82

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

There are a few issues with your templates:

  1. Use the correct directory structure for templates in your apps so that Django knows where to find them:
/ MYapp
   - urls.py
   - views.py
   - ...
   / templates
      / MYapp
         - index.html

/ trevalo_app
  - urls.py
  - views.py
  - ...
  / templates
      / trevalo_app
         - login_user.html
  1. In your views pass the correct template path to the render:
def login_user(request):
     if request.method=='POST':
         ...
     else:
         return render(request,'trevalo_app/login_user.html', {}) # The correct template path

Note that your code fails because in your trevalo_app/login_user.html template you are extending 'MYapp/index.html'. For this to work you must follow the directory structure Django is recommending so that it can find the correct template.

{% extends 'MYapp/index.html' %}
Related