404 when requesting url of saved image in media directory using django rest framework

瘦欲@ 提交于 2019-12-12 05:28:38

问题


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?

来源:https://stackoverflow.com/questions/46120150/404-when-requesting-url-of-saved-image-in-media-directory-using-django-rest-fram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!