如何在Django的不同目录中调用具有相同名称的模板? [英] How do I call templates with same names at different directories in Django?

查看:184
本文介绍了如何在Django的不同目录中调用具有相同名称的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django 1.8

Working with Django 1.8

我有两个同名模板,

base.html /mysite/templates/base.html

从位置相同名称(base.html)的应用程序中,
/mysite/app/templates/base.html

and another from an app with the same name (base.html) at location, /mysite/app/templates/base.html

我的模板设置

my template settings,

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.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',
            ],
        },
    },
]

视图

def home(request):
    c = { name: 'myname', username: 'myusername'}
    return render(request, 'base.html', c)

这会从项目根目录调用 base.html,但是如何从 app调用模板/ templates 目录。

This calls the 'base.html' from the project root, but how can I call the template from the app/template directory.

感谢您的任何参考!

更新:

我已将应用位置添加到模板目录中,

I've added the app location to the template dirs,

'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'app/templates')],

现在我的IDE显示了两个文件,仍然呈现了根目录的 base.html 。如何在一个模板上引用另一个模板?

now my IDE shows both the files, still the root's base.html is rendered. how do I reference one template over the other?

更新:
固定为在应用模板级别添加了一个目录。

Update: Fixed with adding one more directory at the app template level.

现在我的应用程序模板目录结构如下:

now my app templates directory structure looks like this,

mysite / app / templates / app / base.html

os.path.join(BASE_DIR,'app / templates')是如果应用程序位于已安装的应用程序元组中,则在TEMPLATE DIR中不需要。

os.path.join(BASE_DIR, 'app/templates') is not needed in TEMPLATE DIR if the app is in the installed apps tuple.

欢迎在Django 1.8上使用更高效的解决方案。

more efficient solutions on Django 1.8 are welcome.

推荐答案

使用当前目录结构,您不能-通过这种方式可以稍后在 INSTALLED_APPS 中覆盖应用程序的模板设置。惯例是使用类似于以下的目录结构:

With your current directory structure, you can't - it's done this way to allow you to override templates by apps later on in your INSTALLED_APPS setting. The convention is to use a directory structure more like:

app1/templates/app1/base.html
app2/templates/app2/base.html

然后使用 app1 / base.html app2 / base.html 指代您想要的那个。

And then use app1/base.html or app2/base.html to refer to the one you want.

编辑

要更清楚我的意思:您的项目结构可能看起来像这样:

To be more clear on what I mean: your project structure could look like this:

mysite
    - templates
        - base.html
    - manage.py (etc...)
    - app
        - models.py (etc...)
        - templates
            - app
                - base.html

然后,您可以使用 base.html 获取第一个和 app / base.html 来获取另一个-相关的应用程序模板都打包在它们所引用的应用程序内,但具有命名空间。

Then you can use base.html to get the first one and app/base.html to get the other - and the relevant app templates are all packaged inside the app they refer to, but are namespaced.

这篇关于如何在Django的不同目录中调用具有相同名称的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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