Thursday, February 27, 2020

Re: Django Image and File

Thanks for your response. 

On Thu 27 Feb, 2020, 10:03 PM Robert Rajendra, <robert.rajendra@ithands.biz> wrote:
Store only the path in DB and image file in server folder this is the best practice,  save the file in some folder and forward the path to DB Object so that your DB can remember the path of the file and you can load it in the frontend,

here is a snippet that you can use 


import  FileSystemStorage()
from django.core.files.storage import FileSystemStorage
function that will store the file 
profile_pic_upload = request.FILES.get('profile_pic_upload', 'false')
if user:
if profile_pic_upload != 'false':
fs = FileSystemStorage()
profile_pic = compress_image(profile_pic_upload)
filename = fs.save(profile_pic_upload.name, profile_pic)
uploaded_file_url = fs.url(filename)
uploaded_file_url = uploaded_file_url.split("/")
original_url= uploaded_file_url[-1]
user_profile.profile_pic = original_url Here fs is the object of the FileSystemStorege() class compress_image() is my custom function no need to add that, you can directly pass the image object that you are getting from request. then the origina_url will go to DB user_profile.profile_pic = original_url <where user_profile is table and profile_pic is field and original_url is the URL that we get from FileSystemStorage()> also, it will add a randomly generated value at the end of the file that will be very useful if you have 2 images with the same name NOTE make sure that you have a media folder added to you your settings.py file or you can pass the path to where to save the image like this fs = FileSystemStorage(location='media/rent_pictures/') Hope this helps another thing is that image is also a file and treated the same way in the backend regards,

On Thu, 27 Feb 2020 at 11:56, Soumen Khatua <soumenkhatua258@gmail.com> wrote:
Hi Folks,

Actually I have one image filed called profile_pic and another is file field called  documents but I don't know what is best practise to store it. Most importantly how I can make each user details is unique.

Thank you in advance

Regards,
Soumen

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WabzCG%2B4ARdyzdn-3DCckud%3D871RtOSxqRiRMLo6zbQuQ%40mail.gmail.com.


--

Robert Rajendra

Associate Network/Server Support Engineer

IT Hands

P: +91 863.087.3094

W: www.ITHands.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABbC_KHUHtuJRsGUFNqf%2BXK5CZy9oewdueTdjZ6r7A%3DQTTNDQg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WYq%3D1XXyU6q1QxhqgNg%3D1s-y6pFry6WtfmcwVZYY5-Ovg%40mail.gmail.com.

No comments:

Post a Comment