404 when requesting url of saved image in media di

2019-08-16 01:25发布

问题:

I am developing a django rest api using the django rest framework. The rest api manages clothing items to be shown in an android application and has a model named Clothing which has an ImageField for showing the clothing item. From the django admin, I can add an image and save the Clothing model instance by clicked the save button below, which I do click and then get a success message:

I then check my media/clothing/ file which is configured to save the static files and see that the image has indeed been saved in this location:

Now, back to the admin, when I navigate back to the newly created Clothing model instance it has a path showing where the image is currently being stored:

When I click on it I get a 404 page not found (the X's in the url replace the actual numbers):

I'll show my setup for managing static files just in case it is needed:

urls.py

urlpatterns = [
    url(r'^add-clothing/$', views.AddClothingView.as_view(), name="add_clothing_image"),
    url(r'^list-clothing/$', views.ListClothingView.as_view(), name="list_clothing"),
]

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

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'assets'),)

The image is in the correct location when saved from the django admin, however when an attempt to access it from it's url is made, there is a 404, why and how do I fix this?