bleroy
bleroy

Reputation: 311

I need some advice about static and media files

I'm starting to program in Django and need some advice from you.

My project is a catalog containing more than 1000 products. Each product has an image.

I need to optimize the loading time and management of these images and my question is: Where do I store these images? In static folder, in the media folder or another solution?

I read the Django documentation and searched on google on static files and media files, but only found recommendations on "store user uploaded data such profile image in Media folder". But I do not know if this recommendation would apply to my project to optimize loading time and management of these image files.

What are your thoughts on the best way to store these images in my project based on your experience?

I'm using Nginx, DigitalOcean VPS.

Thank you!

Upvotes: 1

Views: 134

Answers (1)

user1600649
user1600649

Reputation:

Static files are part of your theme / skin ("packaging"). These files should generally not change and should be served based upon when the browser last retrieved them and set far ahead in the future:

location /static {
    expires max;
}

Media files are part of the content, not the packaging of the site. It's much more likely that an image changes: the first one uploaded doesn't look that good, better manufacturer supplied media, etc. So it makes more sense to base it's expiration upon the modification date:

location /media {
    # Optional: if rollbacks are frequent
    # if_modified_since before;
    expires modified+1w;
}

Regarding the rollbacks: if it's likely that older versions of an image is restored from backup or versioning systems, you'll want to set this. Note that Django will not set the modification time of an uploaded image to the modification time on the uploader's computer, so a new upload will be a new image, even if it's a previous version.

Now here comes the tricky part: page optimization tools will tell warn you about the expiration time of images not being far in the future and thus will complain about the media files. This is because they can't make a distinction between packaging and content when it comes to images.

Another thing to note is that after that week, the browser will request the images each time and nginx will serve a 304 response when it hasn't changed. Thus your page will generate many more requests. This and the page optimization tools are the two reasons why modified isn't used much for images in the wild and instead only the first policy is used. To deal with modified images, you will then associate a differently named image with the same resource (product) and thus is the responsibility of the content managers or a middleware layer that renames uploaded images to a unique name. One simple trick is to name media images as their md5 or sha1 hash.

Even if you apply a single expiration policy for serving media and static, I would still serve them from different directories. The reason is that static files do not have to be writable by the user running Django (only for the user running the collectstatic management command). This prevents configuration errors or a compromized Django user from messing with the static directory.

Upvotes: 1

Related Questions