python - django 中不存在模板,但我有模板?
问题描述
我有个问题。我使用 django 3.1.1 和 python 3.8.5/ 我尝试在 python + django(后端)和 html、css、bootstrap(前端)中创建一个站点。我创建了两个应用程序 1. 博客(在一个子页面中我将有博客)和 2. 页面其他子页面,如主页。这是我的整个程序https://github.com/Charnel2500/blog_ai_py385_django311
我的博客运行良好(我根据教程创建了这个应用程序) 现在我尝试自己创建另一个应用程序 - “页面”。我创建了一个新应用程序。我在settings.py(和DIR)中添加了这个应用程序
"""
Django settings for ai_blog project.
Generated by 'django-admin startproject' using Django 3.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-%60+qrw+)3c&_tchqlyt8=%k$@b331pw1se*=lsr#21ux7g=$'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'pages',
]
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 = 'ai_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR.joinpath('templates'))], # new
'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',
],
},
},
]
WSGI_APPLICATION = 'ai_blog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
我还创建 pages/urls.py
# pages/urls.py
from django.urls import path
from .views import HomePageView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
]
我创建了 pages/views.py
# pages/views.py
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name = 'home.html'
然后我创建了我的 html 页面 pages/templates/pages/home.html
<!-- templates/home.html -->
<h1>Homepage</h1>
然后我修改我的主要网址 ai_blog/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('home/', include('pages.urls')), # new
]
所以我运行一个服务器,检查站点(http://127.0.0.1:8000/home/),但我在 /home/ 得到 TemplateDoesNotExist 为什么?
解决方案
设置template_name = 'pages/home.html'
而不是template_name = 'home.html'
class HomePageView(TemplateView):
template_name = 'pages/home.html'
推荐阅读
- reactjs - 如何获得选定的颜色
- c++ - 如何从 qml 访问 qabstractlistmodel 派生类对象作为另一个类的属性?
- php - 如何根据命令实时强制重定向用户?
- mongodb - 如何更新 mobgo db 中的集合
- android - WebView 仅适用于模拟器,不适用于设备
- android - 当我尝试在棉花糖版本上打开 MAP 时可能出现未处理的承诺拒绝
- powershell - Get-WindowsUpdateLog 流重定向
- ios - 当作为子视图添加到集合视图单元格时,滚动视图中的内容不可见
- python - Python Polyglot:如何防止连字符分隔属于一起的单词
- java - 有没有一种简单的方法可以将包含秒和毫秒的时间戳值更改为具有小时和分钟的时间戳值?