放置在应用程序目录中的Django 1.3静态文件 [英] Django 1.3 static files placed in application dirs

查看:59
本文介绍了放置在应用程序目录中的Django 1.3静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.3,应用中的静态文件令人困惑。
我在做什么:
1)将

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)服务于我的网址。 py

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)将CSS和js文件放入应用程序目录中的静态文件夹中。
所以我得到了这样的目录树:

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)我将这两个应用程序都添加到settings.py文件中的INSTALLED_APPS中。
现在在我的模板中,当我编写以下行时:

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成功在目录中找到我的CSS文件

Django successfuly finds my css file in directory

my_project/main_app/static/css/

和插头在style.css
中,但是当我写

and plugs in style.css But when I write

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

Django不会插入此文件。

Django doesn't plug in this file.

所以我的问题是:我做错了什么?为什么我不能从secondary_app目录插入CSS文件?
关于这种情况,我有什么要告诉您的?

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?

推荐答案

其背后的想法是,您可以运送所有在 app / static 下具有自己的静态文件的应用程序。

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

在项目中部署应用程序后,运行 python manage.py collectstatic 将复制找到的所有静态文件,这要归功于 STATICFILES_FINDERS 设置(其中包含一个查找器,用于查找<$ STATIC_ROOT 目录中的默认C $ c> static 目录),然后将通过 STATIC_URL

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

开发服务器在 DEBUG时处理 STATIC_URL 模式模式,但这是有趣的内容:在生产环境中提供静态文件

这篇关于放置在应用程序目录中的Django 1.3静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆