I’m working on a django project that’s not mine, but trying to add a local css/js option to the webapp.
In settings_local.py I added this:
if os.path.exists('templates_local/local.css'):
LOCAL_CSS = True
logging.debug('LOCAL_CSS: %s' % LOCAL_CSS)
else:
LOCAL_CSS = False
if os.path.exists('templates_local/local.js'):
LOCAL_JS = True
logging.debug('LOCAL_JS: %s' % LOCAL_JS)
else:
LOCAL_JS = False
which seems to be working (see below). In my main template I added
{% if LOCAL_CSS %}
{% compress css %}
{% include "../templates_local/local.css" %}
{% endcompress %}
{% endif %}
(the project uses django-compressor).
This works as I expect if there is a local.css file present, but if the file is not there I get an error:
Caught TemplateDoesNotExist while rendering: ../templates_local/local.css
In the stack trace LOCAL_CSS is listed as being False. The stack trace unfortunately goes down to show the IF not finding the file (which is expected) but doesn’t seem to include how the IF was evaluated, although it is executing as if it were evaluating to True… but at any rate, it’s not helping me figure out what’s gone wrong.
I also tried explicitly checking that if LOCAL_CSS == True in the event that the if statement above is evaluating as True simply because the variable exists.
At any rate, I’m hoping this is an odd detail I’ve missed about django so far, or something that someone with more experience would see right away what I’ve done wrong.
If you think I’m going about this in the wrong way, feel free to peruse my original question which had no takers: https://stackoverflow.com/questions/11975054/django-recipe-for-local-css-and-local-js-like-settings-local-py-for-app-with-m
answered! – three separate problems
As is often the case, multiple mis-steps seemed to be a simpler single problem
What ended up working was actually pretty simple:
At supervacuo’s suggestions and after some wrangling, I added a context processor, templates_local/context.py:
import os.path
install_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
localcss = os.path.join(install_dir, 'templates_local', 'local.css')
localjs = os.path.join(install_dir, 'templates_local', 'local.js')
def local_static(context):
return {
'LOCAL_CSS': localcss if os.path.exists(localcss) else False,
'LOCAL_JS': localjs if os.path.exists(localjs) else False,
}
and added "rooibos.templates_local.context.local_static", to the pre-existing TEMPLATE_CONTEXT_PROCESSORS list in the settings.py file.
I just had to switch the if statement in the template to an if not:
{% if LOCAL_CSS != False %}
{% compress css %}
{% include LOCAL_CSS %}
{% endcompress %}
{% endif %}
What went wrong?
Problem 1 – I was confused about what was available in the template context
See supervacuo’s 1st suggestion
Problem 2 – Lack of experience/familiarity with the environment
I didn’t catch/suspect that in the context of django of method like os.path.exists() executes from the location of manage.py, not the path/file.py from which you you invoke it (which makes sense in hindsight)
Problem 3 – flawed logic, too simple an approach, maybe something else
I suspect that if TEMPLATE_DEBUG = True you may not be able to have an include with a path string (e.g. {% include "../templates_local/local.css" %}) without getting an error if the file doesn’t exist.
But at any rate, the way it works now (determining that the file exists and then saving either an absolute path OR False as the template variable seems like a more robust solution and is more readable in the template.
Thanks again to supervacuo, your suggestions and comments were really informative – I feel like I learned a good bit about django because of them!
OK, this rabbit hole is getting rather deep (hopefully we can edit the existing question and answer down to something generally-useful once this is all sorted).
Relative paths (like
"local.css") toos.path.exists()are interpreted relative to the directory you were in when you ranrunserver. So, althoughos.path.exists('local.css') == Truein that directory, your debugging stuff needs to specify paths relative tomanage.py(or just make them absolute for simplicity).I’m not sure what
try .. finallyis meant to be doing here, but perhaps just do:Once you’ve confirmed that’s working as expected, build out from there (do your
if os.path.exists()tests inlocal_settings.pyetc.)