Django 3.2.2 上的模板不存在
TemplateDoesNotExist on Django 3.2.2
我在尝试添加新应用程序时遇到以下错误:
TemplateDoesNotExist at /listings/
listings/listings.html
Request Method: GET
Request URL: http://127.0.0.1:8000/listings/
Django Version: 3.2.2
Exception Type: TemplateDoesNotExist
Exception Value:
listings/listings.html
Exception Location: /home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/lib/python3.6/site-packages/django/template/loader.py, line 19, in get_template
Python Executable: /home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/bin/python
Python Version: 3.6.9
Python Path:
['/home/brup/Desktop/Python/Django/FullWebApplication/btre_project',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/lib/python3.6/site-packages']
注意:我浏览了这些链接,但对我没有帮助,我已经检查了这些。我已经添加了页面应用程序并且它工作正常,但是当我尝试添加列表应用程序时它不起作用。
Solution1 Solution2
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',
],
},
},
]
在已安装的应用程序中,我做了以下操作:
INSTALLED_APPS = [
'pages.apps.PagesConfig',
'listings.apps.ListingsConfig',
'realtors.apps.RealtorsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
在urls.py中我添加了以下内容:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('pages.urls')),
path('listings/', include('listings.urls')),
path('admin/', admin.site.urls),
]
上市申请:
正在列表中配置urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='listings'),
path('<int:listing_id>', views.listing, name='listing'),
path('search', views.search, name='search'),
]
配置视图
from django.shortcuts import render
def index(request):
return render(request, 'listings/listings.html')
def listing(request, listing_id):
return render(request, 'listings/listing.html')
def search(request):
return render(request, 'listings/search.html')
内部模板文件夹
您使用大写字母创建了列表 L.but 您在 views.py 中使用了小写字母 l。
我在尝试添加新应用程序时遇到以下错误:
TemplateDoesNotExist at /listings/
listings/listings.html
Request Method: GET
Request URL: http://127.0.0.1:8000/listings/
Django Version: 3.2.2
Exception Type: TemplateDoesNotExist
Exception Value:
listings/listings.html
Exception Location: /home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/lib/python3.6/site-packages/django/template/loader.py, line 19, in get_template
Python Executable: /home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/bin/python
Python Version: 3.6.9
Python Path:
['/home/brup/Desktop/Python/Django/FullWebApplication/btre_project',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/brup/Desktop/Python/Django/FullWebApplication/btre_project/venv/lib/python3.6/site-packages']
注意:我浏览了这些链接,但对我没有帮助,我已经检查了这些。我已经添加了页面应用程序并且它工作正常,但是当我尝试添加列表应用程序时它不起作用。
Solution1 Solution2
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',
],
},
},
]
在已安装的应用程序中,我做了以下操作:
INSTALLED_APPS = [
'pages.apps.PagesConfig',
'listings.apps.ListingsConfig',
'realtors.apps.RealtorsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
在urls.py中我添加了以下内容:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('pages.urls')),
path('listings/', include('listings.urls')),
path('admin/', admin.site.urls),
]
上市申请:
正在列表中配置urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='listings'),
path('<int:listing_id>', views.listing, name='listing'),
path('search', views.search, name='search'),
]
配置视图
from django.shortcuts import render
def index(request):
return render(request, 'listings/listings.html')
def listing(request, listing_id):
return render(request, 'listings/listing.html')
def search(request):
return render(request, 'listings/search.html')
内部模板文件夹
您使用大写字母创建了列表 L.but 您在 views.py 中使用了小写字母 l。