[Fixed]-Variable in setting file using in template

1👍

I’d start by asking – why do you want to do that? It seems like an odd way of… adjusting the templates directories that you’re using, i guess??

I wonder if you may have an XY problem. If you post what you’re trying to achieve, that might be better than a specific solution to this specific problem.

Broadly though, you can only serve django variables (including from settings.py) inside a project or script that’s using Django, or at least its environments. Doing from django.conf import settings imports the settings that are available at the highest level in the project, across all of the apps. Projects are laid out in this rough fashion.

myproject - 
    - myapp1 
    - myapp2 
    - myproject 
        - settings.py
        - wsgi.py 
    - manage.py 

As in this answer, For external scripts, you can still import the django settings thus:

import os
import django
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
# now you can use settings.VARIABLE_NAME

But: this still won’t get it into a template, because getting the variable from here into a template needs you to use the django engine to render a response.

If it’s not being served by the django system, it’s not a template, it’s just a regular html file.

If it’s a template in another app in your overall project, you would pass it as part of your context dictionary, as laid out in the docs on templates.

Leave a comment