Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.3 static files placed in application dirs

Tags:

django

I'm using Django 1.3 and the static files in app is confusing. What I was doing: 1) Set the

STATIC_ROOT = as path to directory 'static' in my project
STATIC_URL = '/static/'

2) Serve in my urls.py

if settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

3) Place css and js files into the folder 'static' in my application directory. So I got such directories tree:

my_project/
    main_app/
        static/
            css/
                style.css
            js/
    secondary_app/
        static/
            foldername/
                file.css

4) I added both of this applications to INSTALLED_APPS in settings.py file. And now in my template when I write follow line:

<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css" />

Django successfuly finds my css file in directory

my_project/main_app/static/css/

and plugs in style.css But when I write

<link rel="stylesheet" href="{{ STATIC_URL }}foldername/file.css" type="text/css" />

Django doesn't plug in this file.

So my question: What I'm doing wrong? Why I cant plug in my css file from secondary_app directory? What I have to tell you more about this situation?

like image 516
tony Avatar asked Apr 07 '11 12:04

tony


1 Answers

The idea behind that is that you ship all your apps with their own static files under app/static.

Once you deploy an app in a project, running python manage.py collectstatic will copy all static files found thanks to STATICFILES_FINDERS setting (which contains a finder looking for a static dir in each installed app by default) within your STATIC_ROOT directory then will be served via STATIC_URL

The development server handles that STATIC_URL pattern when in DEBUG mode but this is an interesting read : Serving static files in production

like image 50
aminho Avatar answered Oct 30 '22 22:10

aminho