开发者

Django 1.3 static files placed in application dirs

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 qu开发者_JS百科estion: 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜