简体   繁体   English

django collectstatic:如何保持项目结构?

[英]django collectstatic: How to keep the project structure?

Currently, collecstatic gathers all files of the project's Apps in one directory (STATIC_ROOT). 当前,collecstatic将项目应用程序的所有文件收集在一个目录(STATIC_ROOT)中。

What should be done to keep the project structure for the static files: STATIC_ROOT/App1 .. STATIC_ROOT/Appn STATIC_ROOT/App3 为保持静态文件的项目结构应执行的操作:STATIC_ROOT / App1 .. STATIC_ROOT / Appn STATIC_ROOT / App3

thanks 谢谢

The documentation recommends namespacing static files : 该文档建议命名静态文件

Static file namespacing 静态文件命名空间

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. 现在,我们也许可以将静态文件直接my_app/static/my_app/static/ (而不是创建另一个my_app子目录),但这实际上是一个坏主意。 Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. Django将使用找到的第一个名称与之匹配的静态文件,如果在其他应用程序中具有相同名称的静态文件,则Django将无法区分它们。 We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. 我们需要能够将Django指向正确的位置,而最简单的方法是通过命名它们来确定它们的间距 That is, by putting those static files inside another directory named for the application itself. 也就是说,通过将这些静态文件放在为应用程序本身命名的另一个目录中。

Make sure to create an inner directory named after your app and put your files in there, eg 确保创建一个以您的应用程序命名的内部目录,并将文件放在其中,例如

  • my_app/
    • static/
      • my_app/ ← This directory my_app/ ←此目录
        • file1
        • file1
        • ... ...

Thank you chris, I find exactly the solution as indicated here: How should I structure my static app files in Django? 谢谢克里斯,我找到了如下所示的解决方案: 应该如何在Django中构建静态应用程序文件?

I have just replace STATICDIR_FILES by : 我刚刚将STATICDIR_FILES替换为:

    STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM