Forums

Serving media files to Django App

I have a news section on my app, where the user can upload a photo for the new. The problem is that the photo only display when DEBUG is set to false. I configured the web tab like this:

URL: /media/

Directory: /home/mgv/myapp/media/news

settings.py

STATIC_URL = "/static/"

STATICFILES_DIRS = [str(BASE_DIR.joinpath("static"))]

STATIC_ROOT = str(BASE_DIR.joinpath("staticfiles"))

MEDIA_URL = "/media/"

MEDIA_ROOT = str(BASE_DIR.joinpath("media"))

model.py

image = models.ImageField(verbose_name=_("Image"), null=False, blank=False, upload_to="news")

When inspecting the site the src atrribute point to the rigth direction and the file is in the folder.

Any help would be appreciated.

When Django is in debug mode, it serves up all of your static content for you -- the system it uses to do that is not particularly fast, and consumes resources that would more usefully used running your code, but it's good enough for development.

When you switch off debug mode, you need to use our (much faster) static files system to serve up that static content -- this help page explains how to do that.