[Django]-Django ImageField not uploading the image

5👍

You need to add static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to urlpattern:

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

See details here.

9👍

There are multiple things that you should check when the image isn’t showing/saving.

  • 1 – You’re missing a request.FILES parameter.

    2 – You’ve made changes to the model but haven’t made migrations yet.

    3 – Django couldn’t create a folder to place the images in(never
    faced this problem)

    4 – There’s a typo in your HTML form; especially here:
    enctype="multipart/form-data"; (I once wrote multipart/form instead of multipart/form-data)

    5 – If you’re using some old Django version then you’d have to
    manually add the following code in your urls.py file: python urlpatterns = [] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    6 – There’s always a possibility that your filename is extremely
    lengthy and it causes some unexpected behavior.

Leave a comment