我对Django非常陌生。我在/处遇到例外TemplateDoesNotExist。 我想我的模板DIR有问题。
以下是网址:
from django.conf.urls import url
from first_app import views
urlpatterns= [
url(r'', views.index, name='index'),
]
以下是视图:
从django.shortcuts导入从django.http导入HttpResponse
def索引(请求): my_dict = {'insert_me':“您好,我已经创建了我的第一个动态模板”} 返回render(request,'Twenty3rdMarch / first_app / templates / index.html',context = my_dict)
这是我的设置:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')
print(TEMPLATES_DIR)
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app',
]
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 = 'Twenty3rdMrach.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_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 = 'Twenty3rdMrach.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'udemy',
'USER':'root',
'PASSWORD':'Ravi@go123',
'HOST':'localhost',
'PORT':'3306',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.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/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
Please help me out to resolve it.
答案 0 :(得分:3)
将Twenty3rdMarch/first_app/templates/index.html
更改为index.html
。从APP_DIRS
引用模板时,您不需要完整路径。
但是我建议您为模板目录正确命名。只需将index.html
放在应用程序的模板目录中,可能会导致模板覆盖。您应在应用程序的模板目录中创建具有相同名称的目录。
myapp
templates
myapp
index.html
然后像myapp/index.html
有关更多信息,请阅读this文档。
答案 1 :(得分:-1)
您可以在主项目的文件夹下创建模板文件夹,然后将其更改为settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
-----rest of settings ------
}]