Question

My django application works well except for one application, all the static files are not loaded.

That happens on this url: http://localhost/db_mgmt/add/dg/. The template is loaded, but no css, no js. When I look at one of the errors, the browser tries to load the page http://localhost/db_mgmt/add/static/jquery.min.js but the link should be: http://localhost/static/jquery.min.js These files are loaded in base.html and work everywhere else...

Example of inclusion in base.html:

<script type="text/javascript" src="{{ STATIC_URL }}jquery.min.js"></script>

Here is my settings.py if it helps:

# Django settings for europolix project.
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

import os
#root of the project
PROJECT_ROOT=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
#root of templates
TEMPLATE_ROOT=os.path.join(PROJECT_ROOT, 'templates')
#root of media files (import / export files)
MEDIA_ROOT=os.path.join(PROJECT_ROOT, 'media')
#root of static files (css, js, jquery...)
STATIC_ROOT=os.path.join(PROJECT_ROOT, 'static')
#WEB_ROOT=url of WSGIScriptAlias given in the apache configuration file (/etc/apache2/apache2.conf in Lubuntu 13.04)
#example: WSGIScriptAlias /europolix /var/www/europolix/europolix/wsgi.py -> WEB_ROOT="/europolix"
#in the apache configuration file, you must update the alias for static files as well
#ex: Alias /europolix/static /var/www/europolix/static -> WEB_ROOT="/europolix"
WEB_ROOT="/europolix"
#local
WEB_ROOT=".."
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.actrence.com/media/", "http://example.com/media/"
MEDIA_URL=WEB_ROOT+'/media/'

# URL prefix for static files.
# Example: "http://media.actrence.com/static/"
STATIC_URL=WEB_ROOT+'/static/'

# Additional locations of static files

STATICFILES_DIRS=(
    STATIC_ROOT,
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

TEMPLATE_DIRS=(
    TEMPLATE_ROOT
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

If I print those variables in my view I have the following paths:

PROJECT_ROOT /var/www/europolix
STATIC_ROOT /var/www/europolix/static
STATIC_URL ../static/
WEB_ROOT ..

Thanks in advance for your help.

Edit :

The variable WEB_ROOT is relative in local. Could it be the problem? The variable searches in the parent directory (am I right?). So the applications with only one "child" in the url work (http://localhost/act or http://localhost/export) but not the applications with many "children" (e.g.: the one above, http://localhost/db_mgmt/add/dg/)

urls.py of the export app:

urlpatterns=patterns('export.views',
    url(r'^/?$', 'export', name='export'),
)

urls.py of the db_mgmt app that does not work well:

urlpatterns=patterns('db_mgmt.views',
    url(r'^add/(?P<field>\w+)/$', 'add', name='add'),
    url(r'^form_add.html/(?P<field>\w+)/$', 'form_add', name='form_add'),
)
Was it helpful?

Solution 2

As I thought, the problem, one of the problems was about the WEB_ROOT variable. Here is how I fixed it:

WEB_ROOT="http://127.0.0.1:8000"

Another problem was the path of the template of my application db_mgmt:

url="/db_mgmt/form_add.html/"+field+"/"

I had to remove the first slash to have it work:

url="db_mgmt/form_add.html/"+field+"/"

Solved :).

OTHER TIPS

We would need to know more about the differences between the applications that work and the application that is giving you trouble

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top