Django provides file system support, so you can easily upload images in an Admin site. To save images in an Admin site, you need to install the Python image manipulation package.
pip install Pillow
1 Configuration
By default, Django saves the uploaded images on the local server and you need to configure the path to save them. We can save the uploaded files in a static file directory, such as the static_files directory we set up earlier Add the following upload and save directory information in the file
MEDIA_ROOT=(BASE_DIR,"static_files/media")
2 Adding an ImageField field to the model class
We add an ImageFiled to the previous BookInfo model class
class BookInfo(): ... image = (upload_to='booktest', verbose_name='Pictures', null=True)
The upload_to option specifies in which subdirectory of the MEDIA_ROOT directory the images of this field are saved for database migration.
python makemigrations python migrate
3 Uploading images using the Admin site
Enter the Admin site's library management page, select a book, you can find an additional upload image field admin site image field, select an image and save, the image will be saved in the static_files/media/booktest/ directory.
In the database, we can see that the image field is set to the path of the image.
The above example of this django using admin site to upload images is all I have to share with you, I hope to give you a reference, and I hope you support me more.