【问题标题】:Why django not able to extend project level template?为什么 django 不能扩展项目级模板?
【发布时间】:2017-03-12 05:59:16
【问题描述】:

这是我的目录:

myproject/
├── frontpage
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   └── intrafish
│   │       └── index.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── myproject
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── base.py
│   │   ├── base.pyc
│   │   ├── development.py
│   │   ├── development.pyc
│   │   └── production.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── requirements.txt
└── templates
    └── base.html

我正在尝试在首页应用程序的 index.html 中扩展 base.html。 我的base.html:

{% load i18n %}
{% load url from future %}
{% load staticfiles %}


{% block header %} {% endblock %}

我的 index.html:

{% extends "base.html" %}

我的 wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "makemeacurry.settings")

application = get_wsgi_application()

我的manage.py:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "makemeacurry.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

我绝对觉得这是因为我将我的设置隔离在不同的文件中,因此发生了这种情况。

我在 base.py 中的模板,我使用 from .base import * 设置在 development.py 中继承:

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = [
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
]


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.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.i18n',

            ],
            'debug': True,
        },
    },
]

我怎样才能做到这一点?

谢谢

---------- ## 编辑 1 ##

这是错误:

TemplateDoesNotExist at /

base.html

这是模板事后的样子:

模板加载器事后分析

Django 尝试按以下顺序加载这些模板:

Using loader django.template.loaders.filesystem.Loader:
    /Users/myuser/projects/myproject/myproject/templates/base.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
    /Users/myuser/.virtualenvs/myproject/lib/python2.7/site-packages/django/contrib/admin/templates/base.html (File does not exist)
    /Users/myuser/.virtualenvs/myproject/lib/python2.7/site-packages/django/contrib/auth/templates/base.html (File does not exist)
    /Users/myuser/projects/myproject/frontpage/templates/base.html (File does not exist)

【问题讨论】:

  • 你还没有说发生了什么,或者那些“模板设置”在哪里以及它们与其他设置文件的关系。
  • @DanielRoseman 我已经通过编辑更新了我的答案,这些模板设置在 base.py 和 development.py 我只是从 .base import *

标签: django python-2.7 django-templates django-1.8


【解决方案1】:

问题是您的设置文件位于子目录中,因此不再正确计算 BASE_DIR;您需要添加另一个级别。

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

【讨论】:

  • 非常感谢...解决了...但是您对 wsgi.py 和 manage.py 设置默认环境配置有什么建议吗?
  • 不,因为我不知道您所说的“设置默认环境配置”是什么意思。
  • 我的意思是这条线 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "makemeacurry.settings") 对吗?它在 wsgi.py 和 manage.py 中。我使用 gunicorn 运行。
猜你喜欢
  • 2018-03-20
  • 2013-06-13
  • 2019-06-26
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
相关资源
最近更新 更多