简体   繁体   中英

Django Loading Templates with Inheritance from Specific Directory

In our project, we have a bunch of different templates that clients to choose from (for their webstore). The file layout is something like this:

templates
    cart.html
    closed.html
    head.html
    standard
        bishop
        default
        indiana
        marley
        mocca
        nihilists
        raconteurs
        tripwire

Every subfolder of standard contains a few template files like base.html, browse.html and item.html. Browse and Item inherit from base.

What I want to do is render the browse template in a specific template folder (let's say templates/standard/bishop) isolated from any other global template path settings in my app. Is there a way to do that?

UPDATE: I'll try to be more clear. If I just render browse.html from the bishop subfolder it tries to extend base.html and it can't find it. I could alter the settings template path to include the bishop folder, but I'm looking for a way to make it work leaving that alone.

In your templates/standard/bishop/browse.html template you're doing the following:

{% extends "base.html" %}

This refers to templates/base.html and not templates/standard/bishop/base.html . By default Django will check your installed applications as well as the template directories that you specified under TEMPLATE_DIRS in settings.py.

This behavior is specified by TEMPLATE_LOADERS in settings.py:

You might be able to get away with what you're trying to do by creating your own template loader, otherwise simply specify the actual path to base.html:

{% extends "standard/bishop/base.html" %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM