当我点击创建职位,它把我带到.../blog/create/
,我得到这个错误:TemplateDoesNotExist at /blog/create/
- 基本. 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>
- 博客/模板/博客/创建. html**
{% extends 'base.html' %}
{% block content %}
<p>Create a new blog...</p>
{% endblock content %}
- 博客/www.example.comurls.py
from django.urls import path
from blog.views import(
create_blog_view,
)
app_name = 'blog'
urlpatterns = [
path('create/', create_blog_view, name="create"),
]
- 主要www.example.comurls.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)
- 博客/www.example.comviews.py
from django.shortcuts import render
from blog.models import BlogPost
def create_blog_view(request):
return render(request, "blog/create.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 %}
- 设置. 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')
我原以为它会把我重定向到新页面,但它显示了一个错误。
3条答案
按热度按时间icnyk63a1#
启用APP_DIRS后,Django会在每个app
/templates/
子文件夹中搜索模板。你有
Template
-大小写错误,最后缺少了s
。所以是的,Django的模板不存在,因为在任何预期的位置都找不到它。mw3dktmi2#
您已获得配置
因此,您需要将Templates目录放在基目录中,而不是放在每个应用程序中,它应该放在
而不是
ttcibm8c3#
显然你的应用程序没有查看
blog
的模板文件夹。它只查看personal
和account
的模板文件夹。你可以在template-loader postmortem
下的附件图片中检查。这通常意味着应用程序没有正确安装。这样的事情有很多原因,其中包括:
./src/
。但似乎不是这样,因为url.py
工作正常。blog
应用程序后忘记保存settings.py
的情况。否则,您可能在某处更改了应用的模板目录。