1

I'm using Django on pythonanywhere to create a webapp. I'm trying to access a file in my media folder by going to http://thedstrat94.pythonanywhere.com/media/example.png. I only get a 'Not Found' error

My Url Patterns in urls.py looks right:

urlpatterns = [
    path('admin/', admin.site.urls), #url(r'^admin/', admin.site.urls),
    path('', views.index, name = "index"),
    path('about/',views.about,name = "about"),
    path('articles/',include("article.urls")),
    path('user/',include("user.urls")),
]
urlpatterns += static(settings.MEDIA_URL, 
document_root=settings.MEDIA_ROOT)

My settings.py file looks right:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
  • Are you running with `DEBUG=True`? See [this other similar post](https://stackoverflow.com/questions/5517950/django-media-url-and-media-root) for how to set it up for serving files locally during development. Using this setup to serve files in production is not advisable, and I think it won't work in a production environment. – Mihai Chelaru May 01 '19 at 23:47
  • This help page explains how to set things up to handle static and media files with DEBUG=False in a production site on PythonAnywhere: https://help.pythonanywhere.com/pages/DjangoStaticFiles/ – Giles Thomas May 02 '19 at 17:55

1 Answers1

1

Django is not made to serve media files in production environment.

Django serves media files only when Debug=True when running application locally in Debug mode.

That means with the above settings you can't serve the media files in production.

Solutions:

  1. Either you use some cloud services like Amazon S3.
  2. You configure your server Apache/Nginx to handle media files.
Astik Anand
  • 12,757
  • 9
  • 41
  • 51