如何告诉Django staticfiles不收集用户上传的媒体目录文件?

时间:2022-04-15 10:36:24

I have a very simple question which I was not able to find an answer. Using Django staticfiles app, I call the statics collect routine by typing this command line : python manage.py collectstatic.

我有一个非常简单的问题,我无法找到答案。使用Django staticfiles应用程序,我通过键入以下命令行来调用静态收集例程:python manage.py collectstatic。

My problem is that I've several applications serving statics + a media directory containing user-uploaded files. Django copies all the files to the static directory, including media files!

我的问题是我有几个服务静态的应用程序+一个包含用户上传文件的媒体目录。 Django将所有文件复制到静态目录,包括媒体文件!

I just would like Django to not copy user-uploaded files to the static directory when I call python manage.py collectstatic. Does anyone have an idea? Tried --ignore option but without success...

我只是希望当我调用python manage.py collectstatic时,Django不会将用户上传的文件复制到静态目录。有没有人有想法?尝试过 - 选择但没有成功......

Thank you in advance.

先感谢您。

2 个解决方案

#1


6  

That was a problem of django.contrib.staticfiles.finders.DefaultStorageFinder, try removing that from STATICFILES_FINDERS.

这是django.contrib.staticfiles.finders.DefaultStorageFinder的问题,请尝试从STATICFILES_FINDERS中删除它。

#2


0  

So what exactly doesn't work if you use --ignore?

那么如果使用--ignore究竟什么不起作用?

Though the real solution is not to mix user generated content and static files.

虽然真正的解决方案不是混合用户生成的内容和静态文件。

If you keep both types separated you can easily put your static files under version control and don't have to deal with excludes on this front as well. And in fact the separation from media (user generated) and static files is the rationale behind staticfiles:

如果将两种类型分开,则可以轻松地将静态文件置于版本控制之下,并且不必在此前面处理排除。实际上,与媒体(用户生成的)和静态文件的分离是静态文件背后的基本原理:

In previous versions of Django, it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL. Part of the purpose of introducing the staticfiles app is to make it easier to keep static files separate from user-uploaded files.

在以前的Django版本中,通常将静态资产与用户上传的文件一起放在MEDIA_ROOT中,并在MEDIA_URL中提供它们。引入staticfiles应用程序的部分目的是使静态文件更容易与用户上传的文件分开。

The docs on this topic are really comprehensive: https://docs.djangoproject.com/en/dev/howto/static-files/

关于这个主题的文档非常全面:https://docs.djangoproject.com/en/dev/howto/static-files/

#1


6  

That was a problem of django.contrib.staticfiles.finders.DefaultStorageFinder, try removing that from STATICFILES_FINDERS.

这是django.contrib.staticfiles.finders.DefaultStorageFinder的问题,请尝试从STATICFILES_FINDERS中删除它。

#2


0  

So what exactly doesn't work if you use --ignore?

那么如果使用--ignore究竟什么不起作用?

Though the real solution is not to mix user generated content and static files.

虽然真正的解决方案不是混合用户生成的内容和静态文件。

If you keep both types separated you can easily put your static files under version control and don't have to deal with excludes on this front as well. And in fact the separation from media (user generated) and static files is the rationale behind staticfiles:

如果将两种类型分开,则可以轻松地将静态文件置于版本控制之下,并且不必在此前面处理排除。实际上,与媒体(用户生成的)和静态文件的分离是静态文件背后的基本原理:

In previous versions of Django, it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL. Part of the purpose of introducing the staticfiles app is to make it easier to keep static files separate from user-uploaded files.

在以前的Django版本中,通常将静态资产与用户上传的文件一起放在MEDIA_ROOT中,并在MEDIA_URL中提供它们。引入staticfiles应用程序的部分目的是使静态文件更容易与用户上传的文件分开。

The docs on this topic are really comprehensive: https://docs.djangoproject.com/en/dev/howto/static-files/

关于这个主题的文档非常全面:https://docs.djangoproject.com/en/dev/howto/static-files/