Django Global base.html模板 [英] Django Global base.html template

查看:74
本文介绍了Django Global base.html模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手.我正在将Django 1.8.6与Python 2.7结合使用.我正在尝试使用一个base.html模板,该模板可以在整个站点(每个应用程序都可以在其中访问)的全局范围内使用.这是我的测试站点的当前结构:

I am new to Django. I am using Django 1.8.6 with Python 2.7. I am trying to use a base.html template that can be used globaly through out the entire site, where every app and access it. Here is my test site's current structure:

twms

投票

移民

静态

模板

项目

移民

静态

模板

项目

index.html

index.html

tmws

静态

模板

tmws

base.html

base.html

这是project/templates/project/index.html的代码

Here is the code for project/templates/project/index.html

{% extends 'tmws/base.html' %}
{% block content %}
    <h1>Projects</h1>
    <ul>
    {% for project in project_list %}
        <li><a href="{% url 'project:detail' project.id %}">{{ project.name }}</a></li>
    {% endfor %}

    </ul>
    end of list
{% endblock %}

这是我收到的错误:

TemplateDoesNotExist位于/​​ project/

TemplateDoesNotExist at /project/

tmws/base.html

tmws/base.html

如何从我的任何应用程序访问tmws/tmws/templates/tmws/base.html?

How do I access tmws/tmws/templates/tmws/base.html from any of my apps?

任何帮助将不胜感激!

让我知道是否需要其他信息.

Let me know if any additional information is needed.

编辑

这是来自settings.py的我的模板设置:

Here are my template settings from settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # If i leave both or just comment one one out I still get the same error
            'tmws.tmws.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',
            ],
        },
    },
]

推荐答案

我认为您的模板目录配置可能有问题.在您的项目的settings.py中,尝试检查您是否具有与此配置类似的配置:

I think you might be having a problem with your template directory configuration. In your project´s settings.py try to check if you have a configuration similar to this one:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'your template dir name')]
        ,
        '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',
            ],
        },
    },
]

您的模板目录名称" 应该为tmws的位置.每当您尝试扩展HTML中的模板时,这将使django搜索该目录.您可以根据需要添加任意多个目录.

Where 'your template dir name' should be tmws. This will make django search for this directory whenever you try to extend templates in your HTML. You can add as many directories as you want.

我认为现在Django必须在以下位置搜索模板:

I think right now Django must be searching for the template in:

twms/project/templates/project

因此,如果您将base.html文件放置在其中,Django便可以找到它.

So maybe if you place your base.html file there Django will be able to find it.

另一个建议是创建一个通用模板目录,并将base.html放在此处,因为您希望在整个站点中使用它.这只是我订购模板的喜好,但无论哪种方式都可以.

Another suggestion would be to create a general templates directory and place your base.html there since you want it to be used in the entire site. This is just my taste for ordering templates, but it should work either way.

在settings.py中添加以下内容解决了该问题.阅读评论以获取更多信息:

Adding the following in settings.py solved the problem. Read comments for more info:

MASTER_BASE_DIR = os.path.dirname(__file__)




TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(MASTER_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 Global base.html模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆