You’re not saving the picture on the way

Asked

Viewed 28 times

-2

I’m creating a field of ImageField no Jango, but when I put to save, it saves in the bank the name of the image but does not save the path to the image.

models:

class Cliente(models.Model):
    photo = models.ImageField(upload_to='static/media/imagens/', blank=True)

Settings:

MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = "/static/"
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'core/static')
]

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

file path:

core
 -static
  -geral
  -media
   -imagens

inserir a descrição da imagem aqui

I hope you help me.

1 answer

0

The field ImageField requires the installation of the library Pillow so that it can validate the image but if you do not want/can install it replace the ImageField for FileField on the model:

...
class Cliente(models.Model):
    photo = models.FileField(upload_to='imagens', blank=True)

By the way, the definition for the media directory is not correct because you are saying that it is in "core/media" and not in "core/Static/media", hence you can put in "Settings.py" something like:

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

If you want to separate static content from media content, leave as is and add "urls.py" to your application at the beginning:

from django.conf import settings

And on the last line...

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Hence Django will also understand it as a directory of static content (i.e., "/Static" and "/media").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.