首页 文章

未检测到Django 1.8应用程序模板

提问于
浏览
1

未加载已更改的应用模板文件,以下是我的设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.request',
            ],
        },
    },
]

和模板结构的屏幕(Url是型号名称):

enter image description here

我错过了什么? (关键字app确实在installed_apps中)

编辑:尝试取消注释#dirs但它仍然无法正常工作 .

Is it possible that this messes things up(on top of settings):

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

如果我删除这一行,它会给出502error . (nginx)

edit 2:

如果我将BASE_DIR更改为:BASE_DIR = os.path.dirname(os.path.abspath( file ))

然后它开始阅读模板,但是从PROJECT而不是app模板文件(django_project / django_project / templates / admin)是否可以更改此内容,以便从app目录中读取?

2 回答

  • 1
    • 您必须了解如何配置模板引擎:

    这是一个简单的设置,告诉Django模板引擎从每个已安装的应用程序内的templates子目录加载模板(APP_DIRS:True):

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'APP_DIRS': True,
            'DIRS': []
        },
    ]
    

    DIRS 列表允许您添加一些Django将搜索模板的其他文件夹

    Django建议为每个应用程序组织这样的模板: django_project/app/templates/app/
    在你的情况下: django_project/keyword/templates/keyword/

    这是您的最终架构:

    django_project
    ├── keyword
    │   ├── migrations
    │   ├── static
    │   ├── templates
    │   │   └── keyword
    │   │       ├── admin
    │   │       │   ├── Url
    │   │       │   │   ├── base_site.html
    │   │       │   │   └── change_list.html
    │   │       │   └── base_site.html
    │   │       ├── base.html
    │   │       ├── embeds.html
    │   │       └── index.html
    │   ├── __init__.py
    │   ├── tests.py
    │   ├── models.py
    │   ├── forms.py
    │   └── views.py
    ├── django_project 
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── manage.py
    

    由于模板引擎在每个 app/templates/ 中搜索,因此在应用视图中,您可以定义模板的相对路径 .

    例如:

    render(request, 'keyword/admin/base_site.html', context)
    
  • 3

    试试这个:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                os.path.join(BASE_DIR, 'templates'),
                os.path.join(BASE_DIR, 'templates/admin'),
                os.path.join(BASE_DIR, 'templates/Url'),
            ],
    

    这样,Django也知道查看 templates 文件夹的子目录 .

相关问题