Django Media Files - Restricting Uploadable File Types with Validators and python-magic

preview_player
Показать описание
In this video, we will look at how to restrict the types of files that can be uploaded by users in our Django applications.

We will see three methods on how to restrict this data:
* Using file input's 'accept' parameter
* Using Django's FileExtensionValidator
* Using python-magic to verify the contents of the file match the extension.

We'll also see how to write a custom validator in Django and add multiple validators to a single Model field.

📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
02:27 File Input accept attribute
06:03 Adding Django FileExtensionValidator to model FileField
10:09 Using python-magic in Django Validator to check file content

☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:

▶️ Full Playlist:

𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:

📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:

#python #django #mediafile
Рекомендации по теме
Комментарии
Автор

Wow, this video is incredibly informative and eye-opening! I had no idea about these. Thank you so much for sharing and raising awareness on such an important topic. Keep up the great work!

SOHAILKHAN-iufu
Автор

Never disappointed with you mate. I learnt somethong new thank you Master 🙌🏾

seydinaoumarsamabaly
Автор

Exactly what I needed. Thanks man, you're the best!

valentino
Автор

Thx a lot.
Small note about unit testing: if you used a generated file for the test you can get a TypeError (expect string but not imagefield file as 10:25 expects a path to the file. I've solved it by changing param to a file for reading bytes.

dodokwak
Автор

Fantastic video! So to be clear on one point: You switched from using an ImageField to a FileField in an earlier video in this series. If I use an ImageField instead, would I get validation functionality out of the box, or do I still have to use "install magic"? I was hoping to use a FileField as you have shown, but I can switch my code up if I am unable to deploy this solution into an Azure production environment.

SteveRobinson-nk
Автор

Hey man I spent a week thinking on how best to do content validation of a file. This was very helpful, once step closer to completing the goal.

cobciqr
Автор

excellent video, congratulations on the didactics

digital-tree
Автор

One more great video, which is not a surprise, thank you! Can you please tell me, considering the options presented, is it enough to use only the 3rd option?

TomislavMiletic
Автор

Excellent! My go to channel for Django :)

iwswordpress
Автор

Thank you for your video.
im curious why just use 1024 bytes in from_buffer?
Is this based on something that data or books?

hjs
Автор

Came back to post another comment, so i have implemented this but when trying to deploy to railway app, the build breaks on this python magic bin library. Any advice?
Is there another way to do the same thing?

TomislavMiletic
welcome to shbcf.ru