首页 > 解决方案 > 在 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

标签: pythondjango

解决方案


模板构造为(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

推荐阅读