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

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

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 &quot;template not found &quot; 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>

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

</body>
</html>`


(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;,
],
},
},
]


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 = [
    &quot;YourAppName&quot;,
    # ...
]
If you don&#39;t do it, your Django project will not be able to &quot;see&quot; the files inside your app."

<details>
<summary>英文:</summary>

Did you add your app in settings? 

    INSTALLED_APPS = [
        &quot;YourAppName&quot;,
        # ...
    ]
If you don&#39;t do it, your Django project will not be able to &quot;see&quot; 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 &#39;templates&#39;, add the given below code

    &#39;DIRS&#39;: [ BASE_DIR / &#39;templates&#39;],



</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:

确定