英文:
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)
`"""
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)
]
`
(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')`
(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>
<h1>The main html page is working </h1>
</body>
</html>`
(below are the changes i made in the settings.py)
`TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]`
ps. ive made sure the spellings are all correct. i am still recieveing an error that says TemplateDoesNotExist at /
main.html
kindly help,
thank you.
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.
Help would be greatly appreciated."
<details>
<summary>英文:</summary>
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)
`"""
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)
]
`
(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')`
(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>
<h1>The main html page is working </h1>
</body>
</html>`
(below are the changes i made in the settings.py)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
ps. ive made sure the spellings are all correct. i am still recieveing an error that says TemplateDoesNotExist at /
main.html
kindly help,
thank you.
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.
Help would be greatly appreciated.
</details>
# 答案1
**得分**: 1
你需要将`main.html`文件放在文件夹`/your_project/your_app/templates/main.html`内。
<details>
<summary>英文:</summary>
You need to have your `main.html` file inside the folder `/your_project/your_app/templates/main.html`.
</details>
# 答案2
**得分**: 0
"Did you add your app in settings?
INSTALLED_APPS = [
"YourAppName",
# ...
]
If you don't do it, your Django project will not be able to "see" the files inside your app."
<details>
<summary>英文:</summary>
Did you add your app in settings?
INSTALLED_APPS = [
"YourAppName",
# ...
]
If you don't do it, your Django project will not be able to "see" the files inside your app.
</details>
# 答案3
**得分**: 0
在您的设置模板配置中,不仅仅添加'templates',还要添加以下代码:
'DIRS': [ BASE_DIR / 'templates'],
<details>
<summary>英文:</summary>
In your settings templates config, instead of just adding 'templates', add the given below code
'DIRS': [ BASE_DIR / 'templates'],
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论