Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Is "collectstatic" supposed to collect media files as well?

I dont know if I am confusing the purpose of collectstatic. Here's my settings module:

# BASE_DIR is the location of my django project folder

STATIC_URL = '/this.static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "ve_static_root")
STATICFILES_DIRS = (

    os.path.join(BASE_DIR, "this.static"),
    os.path.join(BASE_DIR, "this.media"),

                    )
MEDIA_URL = '/this.media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "ve_media_root")

Here, I placed both my static files and media files in STATICFILES_DIRS so that I can use media files as an easy reference when I recall/embed images into my markdown documents, but as you can also see, I made a path for MEDIA_ROOT, which needs to be different from STATIC_ROOT. My concern is that I placed my media folder, this.media, in the STATICFILES_DIRS folder, which allows me to call the images or videos into the Django templates using these staticfile filters, like {% static 'image/file/path/here' %}. As convenient as that is, I also wondered whats the point of the MEDIA_ROOT if all my files, images/videos and web design files, are just going into STATIC_ROOT? As far as I know, Django doesn't have a collectmedia command, so I don't really have anything collecting into the MEDIA_ROOT folder. I just have it out here, empty and all.

Am I missing something about this? Anyone understand Django's perspective on this? Whats your perspective? I am not sure if collectstatic should involve collecting media files as well, especially when I have a MEDIA_ROOT. I looked at the docs on static files and they really arent very helpful with regards to media files.

like image 809
JellisHeRo Avatar asked Aug 06 '15 15:08

JellisHeRo


2 Answers

The Django documentation discourages having the static root and media root as the same directory (having them within the same parent directory is presumably alright). While not fleshed out in the docs specifically, the reason revolves around the inherent uncertainty when it comes to user-generated files. If both static and media files are served from similar directories, then a user, under the right circumstances, might be able to upload any file, which might subsequently be served. This of course creates major security vulnerabilities – this is the motivation for having two separate directories for these two classes of files.

collectstatic relies on the STATICFILES_DIRS array to generate a list of directories through which collectstatic should search for static files. If you are including media files in STATICFILES_DIRS, then, upon running collectstatic (as instigated either by you or by an automated service like Fabric), media and static files will live in the same output directory.

The documentation also has a section about Serving files uploaded by a user during development, which you can do simply by accessing the url at which the media files exist, as you would with any static file. You can simply append

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

to your URL configuration.

django.contrib.staticfiles.views.serve() however, should not be used for deployment. I've written more about this here. It is highly recommended that you serve your static files (and store and serve media files, as necessary) from a directory outside of your project's, in production.

like image 60
ilkahnate Avatar answered Nov 08 '22 05:11

ilkahnate


Media files are files that users upload to your Django site.

Static files are files that you want to serve as part of the site (usually CSS, JavaScript and images).

As such I wouldn't expect collectstatic to do anything with media files.

like image 21
wobbily_col Avatar answered Nov 08 '22 06:11

wobbily_col