Django gunicorn nginx EC2 - media files

Hi,

I have researched Django treatment of media files (vs. static files). Most of the material on the subject assumes the media files are uploaded by the user. What if I have a small collection of images that I want to render - Would I still use MEDIA_ROOT and MEDIA_URL locations to store these files? (Currently, my web app does not contain any stored data.)

If my media files are stored in MEDIA_URL, my understanding is also that these files are NOT included in collectstatic for deployment. So, in prod, are they served from the same directory as in dev? Did I get that right?

Finally, in dev and prod, all my static and media files are visible, but the media files (some small jpg images) are only partially loading (firefox) with the following error: NS_ERROR_NET_PARTIAL_TRANSFER. There is one image that is triggering the following message, but fully loads: NS_BINDING_ABORTED.

Help/feedback/guidance appreciated!

Josh

if os.environ.get("DEBUG", "True") == "True":
    # DEBUG=True
    STATIC_ROOT = os.path.join(BASE_DIR,'App/static')
    STATIC_URL = "App/static/"
else:
    # DEBUG=False
    STATIC_ROOT = os.path.join(BASE_DIR,'static-prod')
    STATIC_URL = "static-prod/"

MEDIA_ROOT = os.path.join(BASE_DIR, 'App/media')
MEDIA_URL = "App/media/"

That depends. Are these always going to be the same files? Or are these files you may be inclined to change, depending upon which installation you have, or some other criteria?

Rather than describing media files as “files uploaded by the user”, think of them as “files supplied after deployment.”

Note: You are also a user - you might be the admin of the site, and so there may be files you want to change post-deployment, without needing to redeploy the site.

If they are files being deployed with your system, then they would be static files and not media files. (For example, a logo for your site might be a static file, if all deployments are going to use the same logo. However, it you’re going to have multiple deployments with different logo files, you might want to make that a media file.)

That is correct.

That is not correct. Again, the idea of these media files are that they’re supplied post-deployment. Because they are files being written by Django, you do not want them being written anywhere within your project directory structure. In what I would consider a “production-quality” deployment, your MEDIA_DIR is a directory set aside for specifically this purpose.

Also keep in mind that in a production-quality deployment, Django would not be serving either media files or static files. That’s something that your webserver would be doing. (See Serving Media files from nginx as one of the topics on here talking about deployments of media and static files.)

Regarding the media file issues, I’d suspect some type of file corruption with how the files are being copied, but that’s pure conjecture.

Very good. Thank you for clarifying! I feel like my images should be treated like true MEDIA files and will adjust accordingly. Yes, I understand that nginx is serving the files in my deployment… When I mentioned prod vs. dev, I meant gunicorn+nginx vs. runserver+nginx, respectively.

I built an amateur (first-time) web app and deployed on a t2-micro instance of EC2, just so I can learn development and deployment. For the time being, a directory outside my project tree would be somewhere like my home dir…

Expanding a bit on your reply, if I wanted to serve a Word doc, for example, for downloading, would it most likely sit in a media directory?

Thanks, again,
Josh