Skip to content Skip to sidebar Skip to footer

Why Image Is Not Getting Saved In Media Directory In Django?

I am trying to save the image in media/images directory and have done each and every neccosry step which is described below. But after all, I am not getting the image in my directo

Solution 1:

Update your MEDIA_ROOT

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

You can read from the documentationpoint number 2

defining the upload_to option specifies a subdirectory of MEDIA_ROOT to be used for uploaded files.

Also make sure that you have added enctype="multipart/form-data"form attribute to your template as mentioned here in the documentation

Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

Also a little change in Add_student view

photo = request.FILES['photo']

Add_Student.save() # NO need to use this. create() creates and saves the object at the same time.

You can read about this here

Post a Comment for "Why Image Is Not Getting Saved In Media Directory In Django?"