简体   繁体   English

TemplateDoesNotExist 在 /blog/create/

[英]TemplateDoesNotExist at /blog/create/

When I click on create post it takes me to .../blog/create/ and I get this error: TemplateDoesNotExist at /blog/create/当我点击创建帖子时,它会将我带到.../blog/create/并且我收到此错误: TemplateDoesNotExist at /blog/create/

在此处输入图像描述

base.html基地.html

<!DOCTYPE html>
<html lang*en>
<head>
    <title>This is the Title</title>
    {% include 'snippets/header.html' %}
</head>
<body>
    <!-- Body -->
    <style type="text/css">
        .main{
            min-height: 100vh;
            height: 100%;
        }
    </style>
    <div class="main">
        {% block content %}

        {% endblock content %}
    </div>

    {% include 'snippets/footer.html' %}
</body>
</html>

blog/Template/blog/create.html博客/模板/博客/create.html

{% extends 'base.html' %}
{% block content %}
    <p>Create a new blog...</p>
{% endblock content %}

blog/urls.py博客/urls.py

from django.urls import path
from blog.views import(
    create_blog_view,
)

app_name = 'blog'

urlpatterns = [
    path('create/', create_blog_view, name="create"),
]

main urls.py主要网址.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from personal.views import (
    home_screen_view,
)

from account.views import (
    registration_view,
    logout_view,
    login_view,
    account_view,
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_screen_view, name="home"),
    path('register/', registration_view, name="register"),
    path('blog/', include('blog.urls', 'blog')),
    path('logout/', logout_view, name="logout"),
    path('login/', login_view, name="login"),
    path('account/', account_view, name="account"),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

blog/views.py博客/views.py

from django.shortcuts import render
from blog.models import BlogPost

def create_blog_view(request):
    return render(request, "blog/create.html", {})

personal/Template/snippets/home.html个人/模板/片段/home.html

{% extends 'base.html' %}

{% block content %}

<style type="text/css">
    @media (max-width: 768px) { 
        .right-column{
            margin-left: 0px;
        }
    }

    @media (min-width: 768px) { 
        .right-column{
            margin-left: 20px;
        }
    }

    .blog-post-container{
        background-color: #fff;
        margin-bottom: 20px;
        width: 100%;
    }
    .create-post-bar{
        background-color: #fff;
        margin-bottom:20px;
    }

    .left-column{
        padding:0px;
    }

    .right-column{
        padding:0px;
    }

</style>

<div class="container">
    <div class="row">
        <div class="create-post-bar d-lg-none col-lg-7 offset-lg-1">
            <a href="{% url 'blog:create' %}">Create post</a>
        </div>

        <div class="left-column col-lg-7 offset-lg-1">
            <div class="blog-post-container">
                <p>Thingy</p>
            </div>

            <div class="blog-post-container">
                <p>Thingy</p>
            </div>

            <div class="blog-post-container">
                <p>Thingy</p>
            </div>
        </div>

        <div class="right-column col-lg-3 d-lg-flex d-none flex-column">
            <div class="create-post-bar">
                <p>Stuff</p>
                <p>Stuff</p>
                <p>Stuff</p>
                <a class="p-2 btn btn-outline-primary" href="{% url 'blog:create' %}">Create post</a>
            </div>
        </div>
    </div>
</div>
{% endblock content %}

settings.py设置.py

from pathlib import Path

import os

BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'personal',
    'account',
    'blog',

    #Djangos apps

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

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',
            ],
        },
    },
]

AUTH_USER_MODEL = 'account.Account'

WSGI_APPLICATION = 'mysite.wsgi.application'
...
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'media'),
]
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_cdn')

I was expecting it to redirect me to the new page but it shows an error.我期待它会将我重定向到新页面,但它显示错误。

With APP_DIRS enabled Django searches for templates inside each app /templates/ subfolder.启用 APP_DIRS Django在每个 app /templates/子文件夹中搜索模板。 Lowercase, plural.小写,复数。

You have Template - wrong case, missing s in the end.你有Template - wrong case, missing s最后。 So yes, the template does not exist for Django because it cannot be found at any expected location.所以是的,Django 的模板不存在,因为无法在任何预期位置找到它。

Apparently your app doesn't look into blog 's template folder.显然,您的应用不会查看blog的模板文件夹。 It only looks into personal and account 's template folders.它只查看personalaccount的模板文件夹。 You can check that in the attached picture under template-loader postmortem .您可以在template-loader postmortem下的附件图片中查看。 It usually means that the app is not installed properly.这通常意味着该应用程序未正确安装。

There are lots of reasons for such a thing, among them:造成这种情况的原因有很多,其中包括:

  • You might have located it in a wrong directory.您可能将其定位在错误的目录中。 The app must be at ./src/ .该应用程序必须位于./src/ It seems that it's not the case as the url.py is working properly.似乎情况并非如此,因为url.py工作正常。
  • It might be the case that you forgot to save settings.py after you added the blog app.可能是您在添加blog应用程序后忘记保存settings.py的情况。
  • It might be your virtual environment.它可能是您的虚拟环境。 A restart and a migrate command might help . 重新启动和迁移命令可能会有所帮助

Otherwise you might have changed the app's template directory somewhere.否则,您可能已经在某处更改了应用程序的模板目录。

You have the configuration你有配置

'DIRS': [os.path.join(BASE_DIR, 'Templates')],

so you need to have the Templates directory in your base directory, not inside each app, it should be所以你需要在你的基本目录中有模板目录,而不是在每个应用程序中,它应该是

Template/blog/create.html

and not并不是

blog/Template/blog/create.html

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

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