简体   繁体   English

找不到模板文件 Django

[英]Template file not found Django

I am trying to make a home page of a new website via Django.我正在尝试通过 Django 创建一个新网站的主页。 My app name is 'blog', home page is home.html I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/我的应用程序名称是“博客”,主页是 home.html 当我转到http://127.0.0.1:8000/blog/home/时仍然收到错误模板不存在

I made sure I added 'blog' to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html我确保我在 settings.py 中的模板中添加了“博客”,并且我在主目录中以及通过 blog/templates/blog/home.html 添加了文件夹模板

myproject/blog/views.py我的项目/博客/views.py

from django.shortcuts import render
from django.http import HttpResponse


def home(request):
    return render(request, 'blog/home.html')

myproject/blog/urls.py我的项目/博客/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('home/', views.home, name='home'),

]

myproject/settings.py我的项目/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, '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',
            ],

myproject/urls.py我的项目/urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

Do you see anything in my code which causes a problem?您是否在我的代码中看到任何导致问题的内容? I receive the message in blog/views.py that "Template file 'blog' not found" on the line我在 blog/views.py 中收到消息“找不到模板文件‘博客’”就行了

return render(request, 'blog/home.html')返回渲染(请求,'blog/home.html')

You are doing it wrong.你做错了。 You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step.您需要仔细阅读 Django 文档,并尝试理解所阅读的内容并逐步实现相同的内容。 The url you have to hit is您必须点击的网址是

http://127.0.0.1:8000/blog/home/

home.html will be rendered at this url. home.html 将在此 url 处呈现。 You don't put html page name in the url你没有把 html 页面名称放在 url 中

I have been looking for answers too and I tried everything but this worked for me on my windows machine.我也一直在寻找答案,我尝试了所有方法,但这在我的 Windows 机器上对我有用。 Adding 'r' before "templates" in os.path.join(BASE_DIR, 'templates') to look like this os.path.join(BASE_DIR, r'templates') resolved the error issue.os.path.join(BASE_DIR, 'templates') "templates" 之前添加 'r' 看起来像这样os.path.join(BASE_DIR, r'templates')解决了错误问题。 Also my templates directory was in my project root along side the parent app and sub-apps.此外,我的模板目录与父应用程序和子应用程序一起位于我的项目根目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM