DEV Community

sium_hossain
sium_hossain

Posted on

3 1

How to serve media files on Django production environment?

I repeat this is not an absolute solution or recommended solution for a large-scale web app. It's just for personal project or low traffic web application.

For high traffic web applications, recommended solution use cloud services like amazon s3 bucket etc for serving media file.

OK! I assume that you know about little bit about nginx. If want to know all about nginx configuration let me know in comment.

Go to your nginx domain file configuration -

sudo nano /etc/nginx/sites-available/<your domain file configuration>
Enter fullscreen mode Exit fullscreen mode
location /static/ {
        #############
    }
location /media/ {
        root #path of your project directory where your media folder exist- example: /home/sium/nmedia_backend/nMedia;
    }
Enter fullscreen mode Exit fullscreen mode

And then save this file and check is there any error in nginx config file by sudo nginx -t and then restart nginx service by sudo systemctl restart nginx

Voila! your media file will serve via nginx. Because DEBUG=FALSE mode means in production mode django won't serve your media file by itself.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (2)

Collapse
 
oscar404m profile image
Linux_Mint • • Edited

when i deploy my django app i get this issue how can i solve it please ?

Collapse
 
siumhossain profile image
sium_hossain • • Edited

django doesn't take care of your media file when debug mode is turn off i mean False. You have to use 3rd party like CDN service or AWS for large application. But if you have low traffic web or personal app you can use above solution.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more