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

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:
ko-fi.com/bugbytes
▶️ Full Playlist:
• Django Media Files
𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
📖 Blog: bugbytes.io/posts/
👾 Github: github.com/bugbytes-io/
🐦 Twitter: / bugbytesio
📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
File Input: developer.mozilla.org/en-US/d...
Accept Attribute: developer.mozilla.org/en-US/d...
Django FileExtensionValidator: docs.djangoproject.com/en/4.1...
Python-magic: pypi.org/project/python-magic/
#python #django #mediafile

Пікірлер: 28

  • @valentino7057
    @valentino7057 Жыл бұрын

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

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thanks a lot for the support!

  • @SOHAILKHAN-iu8fu
    @SOHAILKHAN-iu8fu Жыл бұрын

    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!

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thank you - I'm glad that the video was useful!

  • @codedjango
    @codedjango Жыл бұрын

    This library is really magical 👍

  • @seydinaoumarsamabaly1806
    @seydinaoumarsamabaly1806 Жыл бұрын

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

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thanks a lot man, as always!

  • @dodokwak
    @dodokwak Жыл бұрын

    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.

  • @iwswordpress
    @iwswordpress7 ай бұрын

    Excellent! My go to channel for Django :)

  • @bugbytes3923

    @bugbytes3923

    7 ай бұрын

    Awesome - Thanks a lot for watching!

  • @user-co1bc6iq9r
    @user-co1bc6iq9r Жыл бұрын

    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.

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Hey - thanks, glad to hear it was helpful. Thanks for watching! Good luck in your project.

  • @ganiyevuz
    @ganiyevuz Жыл бұрын

    thanks this was useful for me 👍👍

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thank you for watching!

  • @digital-tree
    @digital-tree Жыл бұрын

    excellent video, congratulations on the didactics

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thanks a lot!

  • @user-wb5hj3qn5x
    @user-wb5hj3qn5x9 ай бұрын

    Thanks man ,very very good

  • @bugbytes3923

    @bugbytes3923

    9 ай бұрын

    Thanks a lot man!

  • @Coden69
    @Coden69 Жыл бұрын

    Wowww

  • @TomislavMiletic
    @TomislavMiletic Жыл бұрын

    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?

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Thank you! I would recommend all 3. Even adding the "accept" attribute in HTML can prevent unwanted uploads that will be rejected on your server. Anything to prevent unnecessary load on the server (plus associated costs) is a win! By only using the 3rd option, users will find it easier to upload the wrong file types, which the server then processes and rejects - that load can be avoided with the other two methods.

  • @TomislavMiletic

    @TomislavMiletic

    Жыл бұрын

    @@bugbytes3923 thank you

  • @frameff9073
    @frameff9073 Жыл бұрын

    thank

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    You're welcome!

  • @hjs9693
    @hjs9693Ай бұрын

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

  • @bugbytes3923

    @bugbytes3923

    Ай бұрын

    Thank you. No real reason to choose that number, just for demo.

  • @TomislavMiletic
    @TomislavMiletic Жыл бұрын

    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?

  • @bugbytes3923

    @bugbytes3923

    Жыл бұрын

    Ah - sorry to hear that. Do you have an error log for this issue? I've not used Railway, but might be able to help.