Error that says -: template does not exist 错误消息:模板不存在

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

Error that says -: template does not exist

问题

以下是您要翻译的内容:

"So , this is my first ever try with django and im currently learning about urls and views, ive been encountering the error that says "template not found " i apologize beforehand if the question is noob-ish.
(here is the code for urls.py file)

  1. `"""
  2. URL configuration for views_urls project.
  3. The `urlpatterns` list routes URLs to views. For more information please see:
  4. https://docs.djangoproject.com/en/4.2/topics/http/urls/
  5. Examples:
  6. Function views
  7. 1. Add an import: from my_app import views
  8. 2. Add a URL to urlpatterns: path('', views.home, name='home')
  9. Class-based views
  10. 1. Add an import: from other_app.views import Home
  11. 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
  12. Including another URLconf
  13. 1. Import the include() function: from django.urls import include, path
  14. 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  15. """
  16. from django.contrib import admin
  17. from django.urls import path
  18. from . import views
  19. urlpatterns = [
  20. path('admin/', admin.site.urls),
  21. path('', views.home)
  22. ]
  23. `

(below is the code for the view.py file that i created , which exists in the same folder as the url.py file)

  1. `from django.shortcuts import render
  2. def home(request):
  3. return render(request, 'main.html')`

(below is the main.html file the exists in a folder called templates)

  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>main</title>
  8. </head>
  9. <body>
  10. <h1>The main html page is working </h1>
  11. </body>
  12. </html>`

(below are the changes i made in the settings.py)

  1. `TEMPLATES = [
  2. {
  3. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4. 'DIRS': ['templates'],
  5. 'APP_DIRS': True,
  6. 'OPTIONS': {
  7. 'context_processors': [
  8. 'django.template.context_processors.debug',
  9. 'django.template.context_processors.request',
  10. 'django.contrib.auth.context_processors.auth',
  11. 'django.contrib.messages.context_processors.messages',
  12. ],
  13. },
  14. },
  15. ]`
  16. ps. ive made sure the spellings are all correct. i am still recieveing an error that says TemplateDoesNotExist at /
  17. main.html
  18. kindly help,
  19. thank you.
  20. What i was expecting was the contents of my main.html file to be displayed, but i got an error that saying template does not exist. Ive tried rechecking if django was successfully installed and it was.
  21. Help would be greatly appreciated."
  22. <details>
  23. <summary>英文:</summary>
  24. So , this is my first ever try with django and im currently learning about urls and views, ive been encountering the error that says &quot;template not found &quot; i apologize beforehand if the question is noob-ish.
  25. (here is the code for urls.py file)

`"""
URL configuration for views_urls project.

The urlpatterns list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home)
]
`

  1. (below is the code for the view.py file that i created , which exists in the same folder as the url.py file)

`from django.shortcuts import render

def home(request):
return render(request, 'main.html')`

  1. (below is the main.html file the exists in a folder called templates)

`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>main</title>

</head>
<body>

  1. &lt;h1&gt;The main html page is working &lt;/h1&gt;

</body>
</html>`

  1. (below are the changes i made in the settings.py)

TEMPLATES = [
{
&#39;BACKEND&#39;: &#39;django.template.backends.django.DjangoTemplates&#39;,
&#39;DIRS&#39;: [&#39;templates&#39;],
&#39;APP_DIRS&#39;: True,
&#39;OPTIONS&#39;: {
&#39;context_processors&#39;: [
&#39;django.template.context_processors.debug&#39;,
&#39;django.template.context_processors.request&#39;,
&#39;django.contrib.auth.context_processors.auth&#39;,
&#39;django.contrib.messages.context_processors.messages&#39;,
],
},
},
]

  1. ps. ive made sure the spellings are all correct. i am still recieveing an error that says TemplateDoesNotExist at /
  2. main.html
  3. kindly help,
  4. thank you.
  5. What i was expecting was the contents of my main.html file to be displayed , but i got an error that saying template does not exist. Ive tried rechecking if django was successfully installed and it was.
  6. Help would be greatly appreciated.
  7. </details>
  8. # 答案1
  9. **得分**: 1
  10. 你需要将`main.html`文件放在文件夹`/your_project/your_app/templates/main.html`内。
  11. <details>
  12. <summary>英文:</summary>
  13. You need to have your `main.html` file inside the folder `/your_project/your_app/templates/main.html`.
  14. </details>
  15. # 答案2
  16. **得分**: 0
  17. "Did you add your app in settings?
  18. INSTALLED_APPS = [
  19. &quot;YourAppName&quot;,
  20. # ...
  21. ]
  22. If you don&#39;t do it, your Django project will not be able to &quot;see&quot; the files inside your app."
  23. <details>
  24. <summary>英文:</summary>
  25. Did you add your app in settings?
  26. INSTALLED_APPS = [
  27. &quot;YourAppName&quot;,
  28. # ...
  29. ]
  30. If you don&#39;t do it, your Django project will not be able to &quot;see&quot; the files inside your app.
  31. </details>
  32. # 答案3
  33. **得分**: 0
  34. 在您的设置模板配置中,不仅仅添加'templates',还要添加以下代码:

'DIRS': [ BASE_DIR / 'templates'],

  1. <details>
  2. <summary>英文:</summary>
  3. In your settings templates config, instead of just adding &#39;templates&#39;, add the given below code
  4. &#39;DIRS&#39;: [ BASE_DIR / &#39;templates&#39;],
  5. </details>

huangapple
  • 本文由 发表于 2023年4月11日 16:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984052.html
匿名

发表评论

匿名网友

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

确定