Django Media Files - Handling User Uploads in Django Forms & Models

preview_player
Показать описание
In this video we'll see how to handle user uploads and media files in a Django project. We'll see how to use Django model ImageField and FileField to store relative paths to files on the file-system, and will see how to create a Django Form class that allows users to upload files.

We'll see what the MEDIA_ROOT and MEDIA_URL settings are for, and how Django can serve media files during development.

Finally, we'll also see how to delete files from the filesystem when the underlying row in the database is deleted, and will discuss the default behaviour of Django in this case.

📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
01:21 Django ImageField and FileField model fields
03:39 Adding Django model with an ImageField
05:27 Adding ModelForm for image uploads
10:10 Handling POST request in view with uploaded file
13:40 Database representation of Django FileField and ImageField
14:25 Creating List view for all uploaded files
17:42 Serving media files with Django
19:33 Changing ImageField upload_to parameter
20:38 Django ImageField additional properties
21:29 Adding delete button to list-view card
26:20 django-cleanup for deleting files attached to model classes
25:52 Overriding the Django model delete method

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

▶️ Full Playlist:

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

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

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

Thanks. I am a Python user, trying to deploy my python project as a web app. Integrating Django with a react frontend has been a new step for me, when it comes to integrating something like dropzonejs. It's nice that your way shows just Django.

claytonbennett
Автор

I did use Django clean up module before but learning simply rewriting delete method was good, nice as always and thanks. Next video will be interesting 👍

atoprak
Автор

Can't wait for the next video : )

raffritter
Автор

great stuff, as always nice and clear, really understandable, these videos are always my go to... thank you

mikedodds
Автор

Perfect tuto for me ! Thank you so much :)

thibaultledanois
Автор

nice one you are really inspiring me in the world of django

djtoon
Автор

great content ! Now we waiting for how to use static files in production.

juniorsemerzier
Автор

Great video. Thx. Looking forward to the next. Tried to upload images on S3 (via django -storages), it works well but I can't delete an uploaded image from the bucket when the "user" wants to delete only the image(partial delete) but not the object (post) entirely.

dodokwak
Автор

This is so clearly presented 🙏Could you explain how to automatically reduce file size uploaded images?

mqbdinu
Автор

17:42 can you make a dedicated video on how to serve static and media file in production using nginx server, also setting up a contabo object storage instead of aws as contabo is cheap and there's no videos on that.

aafan.kuware
Автор

Good work.

Please show us how to host a website in bluehosy

BiologyDaily
Автор

What would be a good way to add multiple images to a dog, but not always either needing to add images or to have a limit, like for a gallery for that instance? Would it be making a model called images that has a foreign key to the dog? Then could you have the form for those images on the same creation form as the dog?

stevemulcahy
Автор

How do we save the images to a cdn? Eg I want users to upload a profile picture, how do they upload it and it gets converted to an image url on cloudinary?

Septumsempra
Автор

Hello, How can i change text lang for the fileinput?

yurigorohov
Автор

You showed me what not to do in production, but you didn't tell me what I should do in production. How do I server files in a production environment?

gibbytravis
Автор

Thank you! Okay so i have an image field model with a default value so that each new category can have something set if a user doesn't upload an image rightaway.

But the problem occures when i test and delete the category. Now that default image gets deleted, so each new category that is created without an image, now has that empty value.

Is there a workaround, but without doing it conditionally in the template?

TomislavMiletic
Автор

Could you please give us some adivise concerning resizing of the uploaded images?
I try to do it on the client side (js) but I'm not sure if it's the best option.
I do want to check the user's data on my server side for the sake of validation. To resize the uploaded image in the view or in the save method of the model has a risk to create a bottleneck. May be to resize it with lambda function on aws? Thank you in advance.

zakchips
Автор

It upload image to folder but not saving to mysql database. pls kindly asist

ayotundeakinwumi
Автор

How to serve in production environment?

alimansimov
Автор

Image upload field works in django admin but not working in template (I am not using django forms)

DevendraSingh-owxp