python - 在 django 中找不到模板 url
问题描述
我有这个项目结构,
foo_project
- templates
- base.html
- photo_list.html
- photo_create.html
- foo_project
- __init__.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- apps
- photo
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- urls.py
- views.py
在apps.photo.views.py
,
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django.views.generic.detail import DetailView
from .models import Photo
class PhotoList(ListView):
model = Photo
template_name_suffix = '_list'
class PhotoCreate(CreateView):
model = Photo
field = ['title', 'body', 'created', 'image']
template_name_suffix = '_create'
success_url = '/
在foo_project/settings.py
from os.path import abspath, dirname, join
BASE_DIR = dirname(dirname(abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
在foo_project/urls.py
,
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
urlpatterns = [
path('', include('apps.photo.urls')),
]
photo.urls
,
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from .views import PhotoList, PhotoCreate, PhotoDelete, PhotoDetail, PhotoUpdate
app_name = 'photo'
urlpatterns = [
path('', PhotoList.as_view(), name='index'),
path('create/', PhotoCreate.as_view(), name='create'),
]
当我点击 时localhost:8000
,它找不到我的模板photo/photo_list.html
,但我没有看到任何丢失。是因为我有子应用结构吗?
我正在使用这些版本,
django=3.0.4
python=3.7.3
解决方案
模板构造为(model_app)/(model_name)(suffix_name).html
。所以对于这个观点,它将是photo/photo_list.html
。因此,这意味着在您的templates
目录下,您需要添加一个具有应用程序名称的目录,并将模板文件放在那里。所以文件树应该是这样的:
foo_project
- templates
- base.html
- foo_project
- __init__.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- apps
- photo
- templates
- photo
- photo_list.html
- photo_create.html
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- urls.py
- views.py
或者你可以在你的项目目录下添加这些templates
:
foo_project
- templates
- base.html
- photo
- photo_list.html
- photo_create.html
- foo_project
- __init__.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- apps
- photo
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- urls.py
- views.py
推荐阅读
- javascript - 安装包“npm install node”失败并出现 E404
- sharepoint - Microsoft SharePoint API 访问被拒绝
- html - 网页图像在移动浏览器上显示为黑色
- android - Android:使用zxing扫描条码并返回片段中的结果
- html - 如何使用 CSS 和 HTML 隐藏高级搜索表单?
- windows - 如何在 Powershell 和 nano 下保存 git commit 消息?(Ctrl+O 不起作用)
- node.js - 将mongo从本地部署到服务器
- javascript - React - 使用 wp_mail 发送电子邮件
- java - Mockito - when ... thenReturn 通过传递期望值列表多次返回
- python-3.x - AttributeError:ResultSet 对象没有属性“get_text”。您可能将元素列表视为单个元素