I accidentally deleted my templates from VS Code, and then recovered them from trash; and that’s when issues began.
The templates were in the app directory /ResilianceRadarR5/ISO22301/templates/ISO22301
after I recovered them by using Finder to move them, I had to move them to a template file folder in the root directory /ResilianceRadarR5/templates/ISO22301 in order for the template files to be found to be rendered.
I changed nothing else in urls.py or views.py; all I did was move the files from teh trash to their original location
In addition, it returns a 404 for logout.html when I try to access teh page from a button
urls.py
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("ISO22301/", include("ISO22301.urls")),
path('accounts/', include('django.contrib.auth.urls')),
]
ISO 22301 urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path("layout/", views.layout, name="layout"),
path("survey/", views.survey, name="survey"),
path("results/", views.results, name="results"),
path('login/', views.user_login, name='login'),
path('generic/', views.generic, name='generic'),
path('introduction', views.introduction, name='introduction'),
path('logout/', views.logout, name='logout'),
]
Relevant views.py
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('introduction')
else:
return redirect('login')
else:
form = LoginForm()
return render(request,'ISO22301/login.html', {'form': form},)
def logout(request):
if request.user.is_authenticated:
print("Logged in")
logout(request)
messages.success(request,('You are logged out'))
return render(request,'ISO22301/logout.html',)
else:
print("Not logged in")
#return redirect('logout')
return render(request,'ISO22301/logout.html',)
Error Message for login.html
TemplateDoesNotExist at /ISO22301/login/
ISO22301/login.html
Request Method: GET
Request URL: http://127.0.0.1:8000/ISO22301/login/
Django Version: 5.1
Exception Type: TemplateDoesNotExist
Exception Value:
ISO22301/login.html
Exception Location: /Users/beyondscorecard/Documents/Work/Python Projects/Dashboard/ResilianceRadarR5/.venv/lib/python3.12/site-packages/django/template/loader.py, line 19, in get_template
Raised during: ISO22301.views.user_login
Python Executable: /Users/beyondscorecard/Documents/Work/Python Projects/Dashboard/ResilianceRadarR5/.venv/bin/python
Python Version: 3.12.3
Python Path:
['/Users/beyondscorecard/Documents/Work/Python '
'Projects/Dashboard/ResilianceRadarR5',
'/Library/Frameworks/Python.framework/Versions/3.12/lib/python312.zip',
'/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12',
'/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload',
'/Users/beyondscorecard/Documents/Work/Python '
'Projects/Dashboard/ResilianceRadarR5/.venv/lib/python3.12/site-packages']
Server time: Mon, 16 Sep 2024 11:54:21 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /Users/beyondscorecard/Documents/Work/Python Projects/Dashboard/ResilianceRadarR5/templates/ISO22301/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/beyondscorecard/Documents/Work/Python Projects/Dashboard/ResilianceRadarR5/.venv/lib/python3.12/site-packages/django/contrib/admin/templates/ISO22301/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/beyondscorecard/Documents/Work/Python Projects/Dashboard/ResilianceRadarR5/.venv/lib/python3.12/site-packages/django/contrib/auth/templates/ISO22301/login.html (Source does not exist)
Using engine jinja2:
This engine did not provide a list of tried templates.
Code in used for logout
<button input type='button' name=Logout onclick="location.href='ISO22301/logout.html'"><lable>Logout</lable></button>
Error message for logout
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/ISO22301/logout.html
Using the URLconf defined in ResilianceRadarR5.urls, Django tried these URL patterns, in this order:
admin/
ISO22301/ layout/ [name='layout']
ISO22301/ survey/ [name='survey']
ISO22301/ results/ [name='results']
ISO22301/ login/ [name='login']
ISO22301/ generic/ [name='generic']
ISO22301/ introduction [name='introduction']
ISO22301/ logout/ [name='logout']
accounts/
The current path, ISO22301/logout.html, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.