9

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?

3
  • You have two directories called "static". Which one does STATIC_ROOT point to again? Commented Apr 7, 2011 at 12:26
  • In the docs on official site I read that developers able to put static files into 'static/' subdirectory of your application. Am I wrong? link Commented Apr 7, 2011 at 12:42
  • By the way if I rename secondary_app/static/foldername/ , which contains my css file, to secondary_app/static/css/ Django plugs in css file successfully. I dont know why... Does anybody can explain this? Commented Apr 8, 2011 at 8:06

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

2 Comments

You mean each time we add a static file into the dev environment we have to run a collectstatic ?
@Glide: No. If you have collectstatic happening but don't want it: remove the Apache alias and add staticfiles_urlpatterns to your urls and turn debug on. docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/… (at the bottom).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.