在 Django 中管理多个应用程序的静态文件
我正在开发一个由许多应用程序组成的 Django (1.3) 项目。每个应用程序在其自己的static
目录下都有自己的静态文件。此外,我添加了一个名为 project_static
的目录,其中应包含各种应用程序(例如 jQuery)中常见的静态文件。
我立即遇到的问题是命名冲突。默认情况下,collectstatic
只会将所有内容放在全局 static
目录下,而不按应用程序对它们进行分类。这对我不起作用,因为每个应用程序都有一个名为 css/screen.css
的文件。
我解决这个问题的方法是从 STATICFILES_FINDERS
中删除 django.contrib.staticfiles.finders.AppDirectoriesFinder
并使用 命名空间静态文件目录,所以我的设置现在看起来像:
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'project_static'),
('my_app', os.path.join(PROJECT_PATH, 'my_app', 'static')),
('another_app', os.path.join(PROJECT_PATH, 'another_app', 'static')),
...
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
问题是这样我就丢失了所有其他应用程序的静态文件(例如django.contrib.admin
)。当然,我可以手动添加管理员,但我不确定如何在不破坏管理员的情况下执行此操作,因为管理员具有不同的目录结构。
有没有一种更好的方法来管理多个应用程序的静态文件?
I am developing a Django (1.3) project composed of many applications. Each application has its own static files under its own static
directory. Moreover, I have added a directory called project_static
which should contain static files which are common throughout the various applications, such as jQuery.
The problem I immediately run into is that of naming collisions. By default, collectstatic
will just put everything under a global static
directory, without classifying them by application. This does not work for me, as each application has - for instance - a file called css/screen.css
.
The way I solved it is by removing django.contrib.staticfiles.finders.AppDirectoriesFinder
from STATICFILES_FINDERS
and using namespaced static files dirs, so my settings now look like:
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'project_static'),
('my_app', os.path.join(PROJECT_PATH, 'my_app', 'static')),
('another_app', os.path.join(PROJECT_PATH, 'another_app', 'static')),
...
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
The problem is that in this way I lose the static files for all other applications (e.g. django.contrib.admin
). Of course I could manually add the admin, but I'm not sure how to do this without breaking the admin, which has a different directory structure.
Is there a better way to manage the static files with more than one application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与使用特定于应用程序的
templates
目录时出现的问题相同。如果您只是将文件直接放在应用中的templates
下,如果两个应用甚至项目级templates
目录使用了同名。解决这个问题的方法是将模板实际放置在templates
目录中应用程序名称的目录中,例如:因此,我将相同的想法应用于静态:
这样,当您运行collectstatic时,每个单个应用程序的静态文件被放置在命名空间目录中。您需要做的唯一更改是在模板中为每个文件添加 app-name 目录前缀。因此,您拥有的不是
{{ STATIC_URL }}css/style.css
,而是{{ STATIC_URL }}my_app/css/style.css
。This is the same problem that occurs with using app-specific
templates
directories. If you just throw the files directly undertemplates
in the app, you'll end up with name collisions if two apps or even the project-leveltemplates
directory utilize templates of the same name. The fix for that is to actually put the templates in a directory of the name of app inside thetemplates
directory like:So, I apply the same idea to static:
That way, when you run collectstatic, each individual app's static files get placed inside a namespaced directory. The only change you need to make is to prefix each of the files with the app-name directory in your templates. So instead of
{{ STATIC_URL }}css/style.css
you have{{ STATIC_URL }}my_app/css/style.css
.