[Answer]-Cannot upload profile picture in django

1👍

Make sure you have set your MEDIA_ROOT in settings, something like this

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

Also make sure that you created first the MEDIA_ROOT directory. Hope it helps.

0👍

Do you created a folder “media” in the root-path of your Django-project?

Here is some information about static files in Django: Django-static-files

In one of my projects I have the following entries in my settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

MEDIA_URL = '/media/'

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

And I also created both foldes in the projects root directory.

Otherwise if this does not work, pleas provide more information about your error-log.

0👍

Include enctype="multipart/form-data" in your form where you want your users to upload picture.

<form class=""  method="post" enctype="multipart/form-data">
  {% csrf_token %}
  {{form.as_table}}
  <input type="submit" name="" value="submit">
</form>

I also had the same requirement as yours and this helped.

Leave a comment