In Django, file uploads are in the form of "setting an address in the DB" and "putting the actual file in the media area"! There is a type that handles Blob type for the time being, but when it comes to operation, it is better to put the actual file on the server and make it accessible at the time of maintenance rather than having the file in the DB. Cool operation is possible ヾ (.> ﹏ <.) ノ ✧ *.
Django has a feature that does all this for you! That is the "** file field **". Just set the type to FileField and set the save destination address in ** upload_to ** and it will be registered automatically!
picture.py
from django.core.validators import FileExtensionValidator
from django.db import models
'''
Image model
@author shibafu
'''
class Picture(models.Model):
id
name = models.CharField(max_length=200)
#Set the file field
image = models.FileField(upload_to='upload_pict/%Y/%m/%d/', #upload_Set the save destination address in to. If you do this, media_upload under root_pict/2020/02/Saves the image to 10
verbose_name='Uploaded image',#It seems to be the name displayed on the management screen. Maybe you don't have to set it
validators=[FileExtensionValidator(['jpg','png','gif', ])],#Extension validator. Error when the extension of the uploaded file is different
)
uploader = models.CharField(max_length=200)
uploadDate = models.DateField()
updateDate = models.DateField()
deleteFlg = models.BooleanField()
def __str__(self):
return '<Picture:id' + str(self.id) + ', ' + \
self.name + '(' + str(self.uploader) + ')>'
I will also make a model form
PictUploadForm.py
'''
Model form for image upload
@author shibafu
'''
class PictUploadForm(forms.ModelForm):
class Meta:
model = Picture
fields = ['name', 'image']
Finally, decide the root directory of the save address and the setting is completed!
setting.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I like it, but I upload it using the model form. In this format, the request object
PictService.py
'''
A service that performs image-related processing
@author shibafu
'''
class PictService:
#Add a new image(For image upload form)
def createFromRequest(self, request):
pictObj = Picture()
pictObj.uploader = "The default guest!"
pictObj.uploadDate = datetime.datetime.today()
pictObj.updateDate = datetime.datetime.today()
pictObj.deleteFlg = False
#Pass both POST and FILE for files
#By setting instance, you can decide the value that can be automatically set on the server side instead of the request
pict:PictUploadForm = PictUploadForm(request.POST, request.FILES, instance=pictObj)
pict.errors
if pict.is_valid():
#Check if the value is correct(Validation)
pict.save()
#Register value in DB
I will actually upload it!
The address is held in the DB
The actual file is saved in the media area!
By creating a function for naming files, you can change the file name when uploading! I was able to implement file upload!
References [Django] How to use the file upload function [Basic settings]
Recommended Posts