I'm uploading images (represented by a FileField) and I need to rename those files when they are uploaded.
I want them to be formated like that:
"%d-%d-%s.%s" % (width, height, md5hash, original_extension)
I've read the documentation but I don't know if I need to write my own FileSystemStorage class or my own FileField class or ... ? Everything is so linked I don't know where to start.
MEDIA_ROOT It takes in the absolute file path of a directory. We have used the same join functions in Django static files tutorial that we have passed in the file path of media directory. This directory will store all the files a user uploads in Django. Although you can directly give the path as a string in MEDIA_ROOT.
Go to your HTML template and add a <form> element. Add the attributes method="post" and enctype="multipart/form-data" . POST is used to send the data to the server while enctype specifies how the form-data should be encoded when sent to the server. Both of these attributes are required to post file uploads.
class SimpleUploadedFile(InMemoryUploadedFile): """ A simple representation of a file, which just has content, size, and a name. """ def __init__(self, name, content, content_type="text/plain"): content = content or b"" super().
You don't need to write your own FileStorage class or anything that complicated.
The 'upload_to' parameter on File/ImageFields can take a function that returns the path/file to use.
How to do this has already been answered here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With