"模板不存在"无法获得解决方案



我从Django开始,但我遇到了这个问题

我在我的VSCode Workspace 中有这个结构

-Project1
__pycache__
__init__.py
asgi.py
settings.py
urls.py
views.py
wsgi.py
-templates
mytemplate.html
db.sqlite3
manage.py

我正在尝试使用我创建的模板

urls.py

from Project1.views import salute
urlpatterns = [
path('admin/', admin.site.urls),
path('salute/', salute),
]

视图.py

from django.template.loader import get_template
from django.shortcuts import render
class Persona(object):

def __init__(self, nombre, apellido):
self.nombre = nombre
self.apellido = apellido
def salute(request): 
p1=Persona('Peter', 'Parker')

temas_del_curso = ['Plantillas', 'Modelos', 'Formularios', 'Vistas', 'Despliegue']
fecha_de_hoy = datetime.datetime.now()
return render(request, 'miplantilla.html', { 'nombre_persona' : p1.nombre, 'apellido_persona' : p1.apellido, 'fecha_de_hoy' : fecha_de_hoy, 'temas' : temas_del_curso })

他们建议我复制保存模板的目录路径

设置.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['C:/Users/machine/Desktop/Django/Project1/templates'],
'APP_DIRS': True,
}

也使用Django建议的形式'DIRS': BASE_DIR / 'templates'和形式[os.path.join (BASE_DIR, 'templates')],但错误继续

这是完整项目的标准设置.py<因为我看不到你们的完整源代码,所以我不能直接解决这个问题>
参考它。

import os
from decouple import config
from unipath import Path
import dj_database_url
BASE_DIR = Path(__file__).parent
CORE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY', default='S#perS3crEt_1122')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=True, cast=bool)
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# load production server from .env
ALLOWED_HOSTS = ['localhost', 'ec2-3-133-208-9.us-east-2.compute.amazonaws.com','www.spreadmore.space','spreadmore.space', config('SERVER', default='127.0.0.1')]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_plotly_dash.apps.DjangoPlotlyDashConfig',
'app',
'authentication', 
'rest_framework',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
'django_plotly_dash.middleware.BaseMiddleware',
'django_plotly_dash.middleware.ExternalRedirectionMiddleware',
]
ROOT_URLCONF = 'core.urls'
LOGIN_REDIRECT_URL = "home"   # Route defined in app/urls.py
LOGOUT_REDIRECT_URL = "home"  # Route defined in app/urls.py
TEMPLATE_DIR = "E:/Python/ocm_project/app/templates"  # ROOT dir for templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'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 = 'core.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME'  : 'E:/Python/ocm_project/db.sqlite3',
}
}
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',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# custom settings
AUTH_USER_MODEL = "authentication.User"
X_FRAME_OPTIONS = 'SAMEORIGIN'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django_plotly_dash.finders.DashAssetFinder',
'django_plotly_dash.finders.DashComponentFinder',
'django_plotly_dash.finders.DashAppDirectoryFinder',
]
# Plotly components containing static content that should
# be handled by the Django staticfiles infrastructure
PLOTLY_COMPONENTS = [
'dash_core_components',
'dash_html_components',
'dash_bootstrap_components',
'dash_renderer',
'dpd_components',
'dpd_static_support',
]
MEDIA_URL = 'app/ocm_data/TW/SPX/'
MEDIA_ROOT = "E:/Python/ocm_project/app/ocm_data/TW/SPX/"

相关内容

最新更新