Forums

What is the best way to serve media files? just let Django handle it or should I use dj-static mediacling?

I tested the performance for both of them, silly thing is that the fastest response is to let Django handle the media files, about 15% faster, but I keep reading that you should move serving media away from django. What is the best practice? excluding using CDN.

We can serve static files for your web app - here are the docs.

Hi glenn, I was asking about media files/user uploaded content, not the static files. Thanks

So? Where the files come from is irrelevant. You're asking about how to serve files and static files are the best solution for that. It doesn't matter if you created them or a user uploaded them.

So everytime a user uploaded a new content, I must restart the web app right? that seems a lot of hassle.

Hi, abirafdirp.

No, you don't have to restart your web app every time an user uploads a new content. You should let PythonAnywhere serve media files for you - that's much better solution than using Django for this purpose. So, to do that you should:

  • in your settings.py set MEDIA_ROOT to some path (like /home/abirafdirp/myproject/media) and MEDIA_URL to /media/ (or whatever URL you want)

  • on a web app tab scroll down to Static files section and make sure there is a row with URL equal to your MEDIA_URL and Directory equal to your MEDIA_ROOT (there should be one by default)

After restarting the web app (once), all user-uploaded content will be correctly served.

I know there is a docs about how to serve media files but I can't understand it enough, forgive me, especially the 'to' part, because english is not my native

If you're using Django's default uploaded files handling, then you'll need to set up a similar static files mapping from MEDIA_URL to MEDIA_ROOT...

and because the description in the static files section says

Files that aren't dynamically generated by your web app, like CSS files and JavaScript, can be served much faster straight off the disk if you specify them here. You need to Reload your web app to activate any changes to your static files.

I immediately assume that that section is not meant for media files. Thanks for telling me what is already in the docs I guess >:-(

Ah. I think I can see why you were confused. That wording is a little bit ambiguous and I think we can make it better based on your feedback.

I like how the conversation started with spice, but ended up with resolution. I was also about to say this "Hi glenn, I was asking about media files/user uploaded content, not the static files. Thanks". And then I continue reading and found the answer. And so I forgot to put the "/media/" in the Web Tab. I just placed the /static/. Now it is working. Then I remember this video, the first video that helped me understand Django and PythonAnywhere at the same time. https://www.youtube.com/watch?v=9Wbfk16jEOk

Cool! thanks for the video!

How Can I Solve Dynamic Uploaded Folders Media, I have more than 2 folders for images how can I do that? I have did this: MEDIA_ROOT = "/home/tanveerahmedst/myblogproject/assets/media" but for further folders in assets.media/...more ?

If you have a set number of folders, just add a static file entry for each folder. If you have Django create new ones, make them all under the same directory and then use the parent directory in a static file mapping and have Django calculate the URLs. If you can't do either of these, then you'll have to get Django to serve your files.

This helped a ton!

:-)

Yea 'Media' and 'Static' mean very different things in the Django terminology. Confusing enough as it is.

True. The way I see it is that media files are the subset of those static files that the user can upload. So you've got "normal" static files like your CSS, images, JavaScript and so on, and then you have these other files that are served up by the same mechanism that users can upload to you (eg. profile images and stuff like that).