user3163650
user3163650

Reputation: 31

Error: requested URL /media/ was not found on this server

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/'

Upvotes: 1

Views: 423

Answers (1)

Astik Anand
Astik Anand

Reputation: 13047

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.

Upvotes: 1

Related Questions