Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 collectstatic 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.

like image 672
elG Avatar asked Mar 12 '23 10:03

elG


1 Answers

Django runserver is not intended for serving up static files in a production environment. It should be limited to development and testing environments.

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.


Static files and media assets are 2 different things.

Static Files

Static files are things like images you created and files that come with 3rd party apps you have installed (e.g. django-cms). These files include images, css and javascript files etc.). So you need to have a settings.STATIC_ROOT for this.

python manage.py collectstatic collects static files from different locations and puts them all in a single folder.

Media Files

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.

Serving up static and media files in production

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 to help you get started serving media and static files in production.

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


Here is a guide from pythonanywhere regarding media and static files: https://help.pythonanywhere.com/pages/DjangoStaticFiles/

like image 168
lukeaus Avatar answered Mar 23 '23 11:03

lukeaus