How do I call templates with same names at different directories in Django?

x0x

Working with Django 1.8

I have two templates with same name,

base.html at /mysite/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',
            ],
        },
    },
]

view

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

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

thank you for any reference!

Update:

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

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

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') is not needed in TEMPLATE DIR if the app is in the installed apps tuple.

more efficient solutions on Django 1.8 are welcome.

Ben

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

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

Edit

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I shim executables with the same names in different directories?

From Dev

How do I use 'is not None' in Django Templates?

From Dev

How do I deal with directories that have spaces in their names from `find`?

From Dev

How do I group companies having different names but are essentially the same semantically?

From Dev

How can I combine 2 or more jquery functions with elements that do the same thing but with different names

From Dev

How do I make configure detect software in different directories?

From Dev

How do I make configure detect software in different directories?

From Dev

How Do I Reference Different Sub-Templates from Different Templates in Meteor?

From Dev

If the same utility script is checked in to different SCM directories yet should be the same everywhere, how can I verify they are the same?

From Dev

How do I call git diff on the same file between 2 different local branches?

From Dev

How do I store different types that implement the same trait in a vector and call common functions on them?

From Dev

How to copy same file from different directories to different directories

From Dev

Comparing (diff) different files with same names in lots of directories and subdirectories

From Dev

How do I separate my templates correctly in Django?

From Dev

Codeigniter: How do I pass multiple inputs with multiple same names?

From Dev

Codeigniter: How do I pass multiple inputs with multiple same names?

From Dev

how do I get a specific value from XML with same names

From Dev

How do I add strings together with different names

From Dev

How do I save ArrayLists with different names in a while loop?

From Dev

How can I run two programs at the same time, but when they are in different directories? (Tcsh shell)

From Dev

How can I copy multiple files in the same directory with different names but same extension in bash?

From Dev

How to find directories containing two different file names?

From Dev

How to call different directive templates based on different kinds of objects in collection?

From Dev

Rsync files directories with different names

From Dev

If a subroutine is in the same directory as my main function, how do I call it?

From Dev

How do I call a function in bash if there is an alias by the same name?

From Dev

How do I call a constructor within a method in the same class?

From Dev

How do I call some part of code again in same function?

From Dev

How do I call map() on an IntStream and return a different type?

Related Related

  1. 1

    How do I shim executables with the same names in different directories?

  2. 2

    How do I use 'is not None' in Django Templates?

  3. 3

    How do I deal with directories that have spaces in their names from `find`?

  4. 4

    How do I group companies having different names but are essentially the same semantically?

  5. 5

    How can I combine 2 or more jquery functions with elements that do the same thing but with different names

  6. 6

    How do I make configure detect software in different directories?

  7. 7

    How do I make configure detect software in different directories?

  8. 8

    How Do I Reference Different Sub-Templates from Different Templates in Meteor?

  9. 9

    If the same utility script is checked in to different SCM directories yet should be the same everywhere, how can I verify they are the same?

  10. 10

    How do I call git diff on the same file between 2 different local branches?

  11. 11

    How do I store different types that implement the same trait in a vector and call common functions on them?

  12. 12

    How to copy same file from different directories to different directories

  13. 13

    Comparing (diff) different files with same names in lots of directories and subdirectories

  14. 14

    How do I separate my templates correctly in Django?

  15. 15

    Codeigniter: How do I pass multiple inputs with multiple same names?

  16. 16

    Codeigniter: How do I pass multiple inputs with multiple same names?

  17. 17

    how do I get a specific value from XML with same names

  18. 18

    How do I add strings together with different names

  19. 19

    How do I save ArrayLists with different names in a while loop?

  20. 20

    How can I run two programs at the same time, but when they are in different directories? (Tcsh shell)

  21. 21

    How can I copy multiple files in the same directory with different names but same extension in bash?

  22. 22

    How to find directories containing two different file names?

  23. 23

    How to call different directive templates based on different kinds of objects in collection?

  24. 24

    Rsync files directories with different names

  25. 25

    If a subroutine is in the same directory as my main function, how do I call it?

  26. 26

    How do I call a function in bash if there is an alias by the same name?

  27. 27

    How do I call a constructor within a method in the same class?

  28. 28

    How do I call some part of code again in same function?

  29. 29

    How do I call map() on an IntStream and return a different type?

HotTag

Archive