Media problems on a production server

Hi,

I created a simple Django app. It works on my local, but after I pulled it to my remote server from GitHub I have a problem - media (pictures) return not found error in a browser. Please help.

in settings.py I put

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

The folder ‘media’ is in project folder (where manage.py is).

And this is my urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
    path('users/', include('users.urls')),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Hello there!

That happens because the Django process only serves static/media files on development, not on production. And most of the times you wouldn’t want the Django process to do that, you want your web server (nginx,apache or other) to handle static content serving.
You must configure your web server to serve these files.
There are several, really up to thousands posts on this forum that explains in detail how this is done. You may want to look at some of them as a starting point, and reach out again if you’re stuck. But don’t worry if this looks challenging at first, this is one of the most confusing aspects of development, because it introduces a lot of new concepts that have nothing to do with the knowledge of python and Django itself. So don’t be affraid to do some research here in the forum to find the answers you’re looking for. One of the greatest skills of a developer is the ability to solve problems, like this one, by researching and digging into the perky details.

Here’s a good starting point.

1 Like