jenishdhameliya
jenishdhameliya

Reputation: 11

uploded image not saved in media folder

settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '../media/images/'

urls.py

if settings.DEBUG:
    # Use static() to add url mapping to serve static files during development (only)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py

    profile_image = models.ImageField(upload_to="../media/images")

file path:

-main_project
   /app             #django app
   /media/images    #media folder inside image folder
   /main            #django main app
   /static/css      #static folder for js,css,images,fonts
   /templates       #html templates

I have also tried other way but still, my image is not uploaded in media folder

Upvotes: 1

Views: 50

Answers (2)

Shreyash mishra
Shreyash mishra

Reputation: 790

firstly change to this

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

and in you model

profile_image = models.ImageField(upload_to="images")

and i guess you are using form to upload the image so in that

<form method="POST" enctype="multipart/form-data">
       

use enctype and if still doesn't work for you try to update it from admin panel than tell me it update or not

Upvotes: 0

Rustam Garayev
Rustam Garayev

Reputation: 2692

Change MEDIA_URL and MEDIA_ROOT to:

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

Once you define above you don't need to explicitly put media folder inside ImageField:

profile_image = models.ImageField(upload_to="images")

See the official docs about managing files

Upvotes: 1

Related Questions