Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:13:06+00:00 2026-06-10T04:13:06+00:00

I’m working on a django project that’s not mine, but trying to add a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:13:08+00:00Added an answer on June 10, 2026 at 4:13 am

    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") to os.path.exists() are interpreted relative to the directory you were in when you ran runserver. So, although os.path.exists('local.css') == True in that directory, your debugging stuff needs to specify paths relative to manage.py (or just make them absolute for simplicity).

    I’m not sure what try .. finally is meant to be doing here, but perhaps just do:

    from django.conf import settings
    import os
    
    def local_static(context):
        # Make sure to return a dictionary
        return {
            'LOCAL_CSS': '/path/to/local.css' if os.path.exists('/path/to/local.css') else False,
            'LOCAL_JS': '/path/to/local.js' if os.path.exists('/path/to/local.js') else False,
            }
    

    Once you’ve confirmed that’s working as expected, build out from there (do your if os.path.exists() tests in local_settings.py etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.