elG elG - 1 year ago 59
Python Question

An easy way to show images in Django on deployment (DEBUG=false)

I am using DJango 1.8 and python 3.4.3, and I have been running my app on Debug mode, and found a way to show images inside a directory configured on MEDIA_ROOT, this was my first question and the solution I have found: How to upload and show images in DJango. But reading the docs I found that that solution is not suitable for a served app, so, if I stop using "Debug=True" the images will not be displayed, and I have to use one of the options exposed on this link: Static files on deployment but I don't have money to pay another server, I just can pay my hosting on pythonanywhere, and for the option to use the same server for the images, I don't have idea how to automate the

and also don't know how to trigger it when an user uploads a new image.

I have used ASP.NET and PHP5 and I didn't had problems with the images in none of them, so, I have two questions:

  1. Is there an easy way to show images URL's?

  2. Is there a high risk security problem if I deploy my app with DEBUG=True?

I hope you can help me, because I find this ridiculous, probably is for better security, but it just not make sense for a framework, instead of making the work easier it make it more difficult and stressful.

Answer Source

Django is not intended for serving up static files in a production environment.

If you are intending to use django's runserver to server up static files with DEBUG=False then use the --insecure flag.

You should never deploy a site with DEBUG = True due to security implications.

Here is a guide from pythonanywhere: https://help.pythonanywhere.com/pages/DjangoStaticFiles/

Static files and media assets are 2 different things.

Static files are things like images you created, (e.g. css, javascript files etc.). So you need to have a settings.STATIC_ROOT for this. collectstatic collects static files from different locations and puts them all in a single folder.

Media files are things the user uploads (e.g. photos, documents etc.). So you have a settings.MEDIA_ROOT for this. collecstatic won't do anything to media files, they will just be there already once the user uploads them.

Frameworks like Django aren't going to cover automatic production server configuration - that is something else you will have to learn unfortunately.

There are a lot of good guides around e.g. this one

Re server costs, I'm sure you can find a host to give you some free credit, or pay $5/month for a server somewhere...