如何在运行Django 4.2服务器时解决’TemplateDoesNotExist’错误?

huangapple go评论91阅读模式
英文:

How to solve 'TemplateDoesNotExist' error in Django 4.2 while running the server?

问题

这是一个Django项目中的错误信息,表明找不到名为 "home.html" 的模板文件。您可能需要检查以下几点以解决此问题:

  1. 确保 "home.html" 文件存在于您的模板文件夹中。根据您的目录结构,它应该在 "F:\Django\geekshows\templates" 目录中。

  2. 确保在您的视图或模板中正确引用了 "home.html"。在您的视图函数中,使用 render 函数的第一个参数应该是模板文件的路径,确保路径是正确的。

  3. 检查您的Django配置。确保您的Django项目设置中的 TEMPLATES 部分已正确配置,包括 'APP_DIRS': True 以便查找应用程序目录中的模板。

  4. 如果您最近修改了模板文件或项目结构,请确保重新启动Django开发服务器,以便使更改生效。

请仔细检查这些方面,以确保 "home.html" 模板文件被正确引用和定位。

英文:
  1. TemplateDoesNotExist at /
  2. home.html
  3. Request Method: GET
  4. Request URL: http://127.0.0.1:5555/
  5. Django Version: 4.2.1
  6. Exception Type: TemplateDoesNotExist
  7. Exception Value:
  8. home.html
  9. Exception Location: C:\Users\soumy\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\loader.py, line 19, in get_template
  10. Raised during: geekshows.views.home
  11. Python Executable: C:\Users\soumy\AppData\Local\Programs\Python\Python311\python.exe
  12. Python Version: 3.11.3
  13. Python Path:
  14. ['F:\\Django\\geekshows',
  15. 'C:\\Users\\soumy\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
  16. 'C:\\Users\\soumy\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
  17. 'C:\\Users\\soumy\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
  18. 'C:\\Users\\soumy\\AppData\\Local\\Programs\\Python\\Python311',
  19. 'C:\\Users\\soumy\\AppData\\Roaming\\Python\\Python311\\site-packages',
  20. 'C:\\Users\\soumy\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages']
  21. Server time: Mon, 22 May 2023 13:56:05 +0000
  22. Template-loader postmortem
  23. Django tried loading these templates, in this order:
  24. Using engine django:
  25. django.template.loaders.filesystem.Loader: F:\Django\geekshows\templates\home.html (Source does not exist)
  26. django.template.loaders.app_directories.Loader: C:\Users\soumy\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\templates\home.html (Source does not exist)
  27. django.template.loaders.app_directories.Loader: C:\Users\soumy\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\templates\home.html (Source does not exist)
  28. django.template.loaders.app_directories.Loader: F:\Django\geekshows\course\templates\home.html (Source does not exist)
  29. django.template.loaders.app_directories.Loader: F:\Django\geekshows\fees\templates\home.html (Source does not exist)

When I am running server this error is comming. I am using Django 4.2 so how to over come.


Folder Structure:

F:\Django\geekshows
course folder and fees folder

fees and geekshows folder

1. course\templates\course\course1.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Document&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;h3&gt;Hello {{nm}}&lt;/h3&gt;
  11. &lt;/body&gt;
  12. &lt;/html&gt;

<!-- end snippet -->

2. course\templates\course\course2.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Document&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;h1&gt;Hello Python&lt;/h1&gt;
  11. &lt;/body&gt;
  12. &lt;/html&gt;

<!-- end snippet -->

3. course\urls.py

  1. from django.urls import path
  2. # from course.views import learn_django
  3. # from fees.views import fees_django
  4. from . import views
  5. urlpatterns = [
  6. path(&#39;learndj/&#39;, views.learn_django),
  7. path(&#39;learnpy/&#39;, views.learn_python)
  8. ]

4. course\views.py

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. from datetime import datetime
  4. # Create your views here.
  5. def learn_django(request):
  6. return render(request, &#39;course/course1.html&#39;, {&#39;nm&#39;:&#39;Django&#39;})
  7. def learn_python(request):
  8. return render(request, &#39;course/course2.html&#39;)

答案1

得分: 1

你似乎有一个名为home的视图,渲染home.html,但找不到home.html

Raised during: geekshows.views.home
...
django.template.loaders.filesystem.Loader: F:\Django\geekshows\templates\home.html (Source does not exist)

请注意,你的应用结构似乎不符合标准,视图文件应该在应用程序内(比如.../geekshows/main/views.py),但你的似乎在项目根文件夹geekshows中。

标准结构:
geekshows/ # 项目根目录
main/ # 应用程序 main(或其他名称)
templates/
main/
home.html
models.py
views.py
...
course/ # 应用程序 course
fees/ # 应用程序 fees
geekshows/ # 与项目同名的文件夹 -> Django 请求的入口点
urls.py
wsgi.py
settings.py
manage.py

请发布视图并显示放置home.html的文件夹结构,以进一步回答你的问题。

英文:

you seem to have a view home that renders home.html but home.html cant be found

  1. Raised during: geekshows.views.home
  2. ...
  3. django.template.loaders.filesystem.Loader: F:\Django\geekshows\templates\home.html (Source does not exist)

please note that you seem to have a non standard structure of your app as a view file should be inside an app (like .../geekshows/main/views.py) but yours seems to be in the project root folder geekshows.

standard structure:

  1. geekshows/ # project root
  2. main/ # app main (or another name)
  3. templates/
  4. main/
  5. home.html
  6. models.py
  7. views.py
  8. ...
  9. course/ # app course
  10. fees/ # app fees
  11. geekshows/ # folder with same name as project -&gt; django entry point for requests
  12. urls.py
  13. wsgi.py
  14. settings.py
  15. manage.py

please post the view and show the folder structure where you placed home.html to further answer your question

答案2

得分: 0

Sure, here are the translated parts:

5. geekshows\templates\home.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <h3>Home {{name}}</h3>
  11. </body>
  12. </html>

6. geekshows\settings.py

  1. """
  2. Django settings for geekshows project.
  3. Generated by 'django-admin startproject' using Django 4.2.1.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/4.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/4.2/ref/settings/
  8. """
  9. from pathlib import Path
  10. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  11. BASE_DIR = Path(__file__).resolve().parent.parent
  12. TEMPLATES_DIR = BASE_DIR / "templates"
  13. # Quick-start development settings - unsuitable for production
  14. # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
  15. # SECURITY WARNING: keep the secret key used in production secret!
  16. SECRET_KEY = 'django-insecure-0q$2kj0_mvxvfz!w((9#-qp+_ceqkdcl+g9^_zq^c$7$ew1h&y'
  17. # SECURITY WARNING: don't run with debug turned on in production!
  18. DEBUG = True
  19. ALLOWED_HOSTS = []
  20. # Application definition
  21. INSTALLED_APPS = [
  22. 'django.contrib.admin',
  23. 'django.contrib.auth',
  24. 'django.contrib.contenttypes',
  25. 'django.contrib.sessions',
  26. 'django.contrib.messages',
  27. 'django.contrib.staticfiles',
  28. 'course',
  29. 'fees',
  30. 'result',
  31. ]
  32. MIDDLEWARE = [
  33. 'django.middleware.security.SecurityMiddleware',
  34. 'django.contrib.sessions.middleware.SessionMiddleware',
  35. 'django.middleware.common.CommonMiddleware',
  36. 'django.middleware.csrf.CsrfViewMiddleware',
  37. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  38. 'django.contrib.messages.middleware.MessageMiddleware',
  39. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  40. ]
  41. ROOT_URLCONF = 'geekshows.urls'
  42. TEMPLATES = [
  43. {
  44. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  45. 'DIRS': [TEMPLATES_DIR],
  46. 'APP_DIRS': True,
  47. 'OPTIONS': {
  48. 'context_processors': [
  49. 'django.template.context_processors.debug',
  50. 'django.template.context_processors.request',
  51. 'django.contrib.auth.context_processors.auth',
  52. 'django.contrib.messages.context_processors.messages',
  53. ],
  54. },
  55. },
  56. ]
  57. WSGI_APPLICATION = 'geekshows.wsgi.application'
  58. # Database
  59. # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
  60. DATABASES = {
  61. 'default': {
  62. 'ENGINE': 'django.db.backends.sqlite3',
  63. 'NAME': BASE_DIR / 'db.sqlite3',
  64. }
  65. }
  66. # Password validation
  67. # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
  68. AUTH_PASSWORD_VALIDATORS = [
  69. {
  70. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  71. },
  72. {
  73. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  74. },
  75. {
  76. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  77. },
  78. {
  79. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  80. },
  81. ]
  82. # Internationalization
  83. # https://docs.djangoproject.com/en/4.2/topics/i18n/
  84. LANGUAGE_CODE = 'en-us'
  85. TIME_ZONE = 'UTC'
  86. USE_I18N = True
  87. USE_TZ = True
  88. # Static files (CSS, JavaScript, Images)
  89. # https://docs.djangoproject.com/en/4.2/howto/static-files/
  90. STATIC_URL = 'static/'
  91. # Default primary key field type
  92. # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
  93. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

7. geekshows\urls.py

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from . import views
  4. urlpatterns = [
  5. path('admin/', admin.site.urls),
  6. path('cor/', include('course.urls')),
  7. path('fe/', include('fees.urls')),
  8. path('', views.home),
  9. ]

8. geekshows\views.py

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. def home(request):
  4. return render(request, 'home.html', {'name':'Page'})

Please note that the code and HTML content have been translated as requested, and there are no additional comments or explanations.

英文:

This is a answer to Razenstein

5. geekshows\templates\home.html

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Document&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;h3&gt;Home {{name}}&lt;/h3&gt;
  11. &lt;/body&gt;
  12. &lt;/html&gt;

6. geekshows\settings.py

  1. &quot;&quot;&quot;
  2. Django settings for geekshows project.
  3. Generated by &#39;django-admin startproject&#39; using Django 4.2.1.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/4.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/4.2/ref/settings/
  8. &quot;&quot;&quot;
  9. from pathlib import Path
  10. # Build paths inside the project like this: BASE_DIR / &#39;subdir&#39;.
  11. BASE_DIR = Path(__file__).resolve().parent.parent
  12. TEMPLATES_DIR = BASE_DIR / &quot;templates&quot;
  13. # Quick-start development settings - unsuitable for production
  14. # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
  15. # SECURITY WARNING: keep the secret key used in production secret!
  16. SECRET_KEY = &#39;django-insecure-0q$2kj0_mvxvfz!w((9#-qp+_ceqkdcl+g9^_zq^c$7$ew1h&amp;y&#39;
  17. # SECURITY WARNING: don&#39;t run with debug turned on in production!
  18. DEBUG = True
  19. ALLOWED_HOSTS = []
  20. # Application definition
  21. INSTALLED_APPS = [
  22. &#39;django.contrib.admin&#39;,
  23. &#39;django.contrib.auth&#39;,
  24. &#39;django.contrib.contenttypes&#39;,
  25. &#39;django.contrib.sessions&#39;,
  26. &#39;django.contrib.messages&#39;,
  27. &#39;django.contrib.staticfiles&#39;,
  28. &#39;course&#39;,
  29. &#39;fees&#39;,
  30. &#39;result&#39;,
  31. ]
  32. MIDDLEWARE = [
  33. &#39;django.middleware.security.SecurityMiddleware&#39;,
  34. &#39;django.contrib.sessions.middleware.SessionMiddleware&#39;,
  35. &#39;django.middleware.common.CommonMiddleware&#39;,
  36. &#39;django.middleware.csrf.CsrfViewMiddleware&#39;,
  37. &#39;django.contrib.auth.middleware.AuthenticationMiddleware&#39;,
  38. &#39;django.contrib.messages.middleware.MessageMiddleware&#39;,
  39. &#39;django.middleware.clickjacking.XFrameOptionsMiddleware&#39;,
  40. ]
  41. ROOT_URLCONF = &#39;geekshows.urls&#39;
  42. TEMPLATES = [
  43. {
  44. &#39;BACKEND&#39;: &#39;django.template.backends.django.DjangoTemplates&#39;,
  45. &#39;DIRS&#39;: [TEMPLATES_DIR],
  46. &#39;APP_DIRS&#39;: True,
  47. &#39;OPTIONS&#39;: {
  48. &#39;context_processors&#39;: [
  49. &#39;django.template.context_processors.debug&#39;,
  50. &#39;django.template.context_processors.request&#39;,
  51. &#39;django.contrib.auth.context_processors.auth&#39;,
  52. &#39;django.contrib.messages.context_processors.messages&#39;,
  53. ],
  54. },
  55. },
  56. ]
  57. WSGI_APPLICATION = &#39;geekshows.wsgi.application&#39;
  58. # Database
  59. # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
  60. DATABASES = {
  61. &#39;default&#39;: {
  62. &#39;ENGINE&#39;: &#39;django.db.backends.sqlite3&#39;,
  63. &#39;NAME&#39;: BASE_DIR / &#39;db.sqlite3&#39;,
  64. }
  65. }
  66. # Password validation
  67. # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
  68. AUTH_PASSWORD_VALIDATORS = [
  69. {
  70. &#39;NAME&#39;: &#39;django.contrib.auth.password_validation.UserAttributeSimilarityValidator&#39;,
  71. },
  72. {
  73. &#39;NAME&#39;: &#39;django.contrib.auth.password_validation.MinimumLengthValidator&#39;,
  74. },
  75. {
  76. &#39;NAME&#39;: &#39;django.contrib.auth.password_validation.CommonPasswordValidator&#39;,
  77. },
  78. {
  79. &#39;NAME&#39;: &#39;django.contrib.auth.password_validation.NumericPasswordValidator&#39;,
  80. },
  81. ]
  82. # Internationalization
  83. # https://docs.djangoproject.com/en/4.2/topics/i18n/
  84. LANGUAGE_CODE = &#39;en-us&#39;
  85. TIME_ZONE = &#39;UTC&#39;
  86. USE_I18N = True
  87. USE_TZ = True
  88. # Static files (CSS, JavaScript, Images)
  89. # https://docs.djangoproject.com/en/4.2/howto/static-files/
  90. STATIC_URL = &#39;static/&#39;
  91. # Default primary key field type
  92. # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
  93. DEFAULT_AUTO_FIELD = &#39;django.db.models.BigAutoField&#39;

7. geekshows\urls.py

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from . import views
  4. urlpatterns = [
  5. path(&#39;admin/&#39;, admin.site.urls),
  6. path(&#39;cor/&#39;, include(&#39;course.urls&#39;)),
  7. path(&#39;fe/&#39;, include(&#39;fees.urls&#39;)),
  8. path(&#39;&#39;, views.home),
  9. ]

8. geekshows\views.py

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. def home(request):
  4. return render(request, &#39;home.html&#39;, {&#39;name&#39;:&#39;Page&#39;})

huangapple
  • 本文由 发表于 2023年5月22日 22:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定