Upload an image with django 2.0

0

I need to upload an image in the administration site or in another html page, I have a model which has an ImageField attribute, I need once it is assigned an image to be able to visualize it. Thanks in advance to any help

    
asked by Leandro L 13.09.2018 в 19:42
source

1 answer

0

First of all you have to create a directory called media at the root of your project, that is, where all your apps and the manage.py file are

After that you have to go to the settings file and at the end of it all put the following lines:

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

Inside the middle folder you must create another directory to save the images of the model. You can put the name of the app where you have the model. If your app is called projects, you create the projects directory in media. That is / media / projects.

Then your Imagafield field you leave it like this:

image = models.ImageField(verbose_name="Imagen", upload_to="proyectos")

As you can see, the upload_to attribute points to the directory you'll create in / media

I hope it serves you. Greetings

Doing all this, you will see the image in the administration panel.

    
answered by 21.09.2018 в 22:34