M
M
Marin Danila2020-04-18 01:44:50
Django
Marin Danila, 2020-04-18 01:44:50

Django It seems to be looking for a template in the wrong place or a function is written incorrectly, what should I do?

In general, I tried to create my first site on Django (Django version 3.0.5 Python version 3.8.2). The course was quite outdated but any minor problems were quickly resolved.
Folder structure:

mysite\ mainApp

\

__pycache__
migrations\

__pycache__

mysite\

__pycache__
tamplates

from django.contrib import admin
from django.urls import path, include 

urlpatterns = [
    path(r'admin/', admin.site.urls),
   <b>path(r'^$', include('mainApp.urls')),</b>
    ]

This is the urls.py file, the link that is supposed to open the main page is highlighted, it launches another urls.py file in the mainApp folder
from django.urls import path, include 
from . import views

urlpatterns = [
  path('^$', views.index, name='index')
]

this calls the index function from the views.py file
from django.shortcuts import render

def index(request):
  return render(request, 'mainApp/homePage.html')

And here the most interesting begins. When I run the site on a local server, I get a 404 error, although in theory if you enter path(r'^$') the quotes should be considered empty, but in the new version you just need to leave them empty (which I did). But this resulted in errors
TemplateDoesNotExist at /
mainApp/homePage.html
Template-loader postmortem
Django tried loading these templates, in this order:

Using django engine:

django.template.loaders.app_directories.Loader: C:\Users\elena\AppData\ Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\admin\templates\mainApp\homePage.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\templates\mainApp\homePage.html (Source does not exist)

Traceback
C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) …
▶ Local vars
C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) …
▶ Local vars
C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
E:\Projects\mysite\mainApp\views.py in index
return render(request, 'mainApp/homePage.html') …
▶ Local vars
C: \Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\shortcuts.py in render
content = loader.render_to_string(template_name, context, request, using=using) …
▶ Local vars
C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py in render_to_string
template = get_template(template_name, using=using) …
▶ Local vars
C:\Users\Elena\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py in get_template
raise TemplateDoesNotExist(template_name, chain=chain) …
▶ Local vars

I tried to bypass problem and not solve it and just instead of leaving single quotes empty, I entered 'main' there. But it didn't help, when I ran already 127.0.0.1:8000/main a 404 error was issued and this was written in the list of existing pages:
admin/
main main [name='index']
The current path, main/, didn't match any of these.
I thought that the error is most likely in the index function, I looked and did not understand what the problem was. I read that there may be a problem with the fact that django cannot find html files in the templates folder.
In the settings.py file in TEMPLATES_DIRS I wrote os.path.join(BASE_DIR, 'templates'
I checked that import os is at the beginning of the file.
After that, it began to give 3 errors

Using engine django:

django.template.loaders.filesystem.Loader: E: \Projects\mysite\templates\mainApp\homePage.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\elena\AppData\Local\Programs\Python\Python38-32\lib\site -packages\django\contrib\admin\templates\mainApp\homePage.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\elena\AppData\Local\Programs\Python\Python38-32 \lib\site-packages\django\contrib\auth\templates\mainApp\homePage.html (Source does not exist)

In Traceback everything seems to be the same

And there was nothing else I could do except to check the spelling of the names, but this did not lead to anything.
In INSTALLED_APPS, I also checked whether I connected the mainApp folder (Yes, I did), tried to change the name of the function and I don’t know what to do anymore.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislava, 2021-12-28
@vladis005

Check how you connected the templates. Or leave the quotes not empty and insert / into them. Better yet, update django to version 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question