One of the most common requirement in any modern web application is the ability to take images or pictures from the users as input and save them on the server however Letting users upload files can have big security implications. In this article, we will learn how to upload images in a Django application. Uploading Images in Django Django has two model fields that allow user uploads FileField and ImageField basically ImageField is a specialized version of FileField that uses Pillow to confirm that a file is an image. Let’s, start by creating models. models.py from django.db import models class Image(models.Model): title = models.CharField(max_length=200) image = models.ImageField(upload_to='images') def __str__(self): return self.title The image column is an ImageField field that works with the Django’s file storage API, which provides a way to store and retrieve files, as well as read and write them. The upload_to parameters specify the location where images will be stored which for this model is MEDIA_ROOT/images/ Setting dynamic paths for the pictures is also possible. image = models.ImageField(upload_to='users/%Y/%m/%d/', blank=True) This will store the images in date archives …
The post How To Upload Images With Django appeared first on Django Central.
from Planet Python
via read more
No comments:
Post a Comment