0

Background:

my setting.py

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    ('assets/css','/var/www/mongo_conprima/static_files/css/'),
    ('assets/js','/var/www/mongo_conprima/static_files/js/') )
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    #'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

I am able to server static file in template this way

<script src="{% static "assets/SlickGrid-master/lib/firebugx.js" %}"></script>

Problem: there are images inside my js files with relative paths which fail to load like this

/var/www/mongo_conprima/static_files/SlickGrid-master/slick.formatters.js:
   55  
   56    function CheckmarkFormatter(row, cell, value, columnDef, dataContext) {
   57:     return value ? "<img src='/images/tick.png'>" : "";
   58    }
   59  })(jQuery);

ERROR: I get following errors in console

GET http://127.0.0.1:8000/images/tick.png 404 (NOT FOUND) slick.grid.js:2567

3 Answers 3

2

You can probably use global variable in your template and use it in your js files

Template:

<html>
<script>
   var GLOBAL_PATH = '{{ STATIC_PATH }}'; 
</script>
</html>

JS:

return value ? "<img src='" + GLOBAL_PATH + "/tick.png'>";
Sign up to request clarification or add additional context in comments.

Comments

0

Use the static tag inside your function as well, so the javascript should look like

function CheckmarkFormatter(row, cell, value, columnDef, dataContext) {
    return value ? "<img src='{% static "/images/tick.png" %}'>" : "";
}

The static tag will give you full the path to static file.

1 Comment

I can not go on changing the urls in all js files
0

A couple of solutions to this issue have been answered in the following question: https://stackoverflow.com/a/6574670/2487431

You can either add a config to the webserver that will invisibly redirect the paths to the correct static path or you can use the urls.py to add the redirect.

The link explains in depth how you can do this. Hopefully this helps someone in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.