About Server Images and media folder

16 views
Skip to first unread message

Aditya Desle

unread,
Jun 14, 2023, 12:19:24 PM6/14/23
to Django users
Hello Everyone, 
I have a React + Django App.
Deployed on digital ocean server/droplet.

Everything is working fine on local system and server too. But the issue i'm facing is about images.
I have multiple folder in media folder and the when i call the post request to add the images, the images doesn't get stored in folder 
I have applied and configured the nginx file too.
Still facing same issue 
The post request on server side get called perfectly still the images aren't getting stored inside the folder.




ivan harold

unread,
Aug 9, 2023, 11:08:29 AM8/9/23
to Django users
Some raw codes that you can use as reference 

class UserProfile (models.Model): user_img = models.ImageField(null=True, blank=True)

def edit_profile(request):
    try:
        # checking if the user exist in UserProfile through the logged in email id
        user_data = UserProfile.objects.get(emailID = request.user.email)
        if request.method == "POST" and request.FILES:
            name = UserProfile.objects.all()
            response_data = {}
            # user profile image start
            user_img = request.FILES['user_img']
            # user profile image end
            name = request.user.username
            emailID = request.user.email
            phone = request.POST.get('phone')
            college_name = request.POST.get('college_name')
            branch = request.POST.get('branch')

            response_data['user_img'] = user_img
            response_data['name'] = name
            response_data['emailID'] = emailID
            response_data['phone'] = phone
            response_data['college_name'] = college_name
            response_data['branch'] = branch
            # updating the current logged in user values
            user_data = UserProfile.objects.get(emailID = request.user.email)
            if(user_data.emailID == request.user.email):
                user=UserProfile.objects.get(emailID = request.user.email)
                user.user_img = user_img,
                user.name = name,
                user.emailID = emailID,
                user.phone = phone,
                user.college_name = college_name,
                user.branch = branch
                user.save()
               
            return render(request, 'edit_profile.html')
    except UserProfile.DoesNotExist:
        name = UserProfile.objects.all()
        response_data = {}
        # creating new user
        if request.POST.get('action') == 'post' and request.FILES:
            # user profile image start
            user_img = request.FILES['user_img']
            # user profile image end
            name = request.user.username
            emailID = request.user.email
            phone = request.POST.get('phone')
            college_name = request.POST.get('college_name')
            branch = request.POST.get('branch')

            response_data['user_img'] = user_img
            response_data['name'] = name
            response_data['emailID'] = emailID
            response_data['phone'] = phone
            response_data['college_name'] = college_name
            response_data['branch'] = branch
            try:
                # checking if the user exist
                user_data = UserProfile.objects.get(emailID = request.user.email)
            except UserProfile.DoesNotExist:
                # if the user doesn't exist create the user
                UserProfile.objects.create(
                    user_img = user_img,
                    name = name,
                    emailID = emailID,
                    phone = phone,
                    college_name = college_name,
                    branch = branch
                )
            return render(request, 'edit_profile.html')
    else:
        # if the profile is already created fetch the values
        context = {
            'user_img' : user_data.user_img.url,
            'name' : user_data.name,
            'emailID' : user_data.emailID,
            'phone' : user_data.phone,
            'college_name' : user_data.college_name,
            'branch' : user_data.branch
            }
        return render(request, 'edit_profile.html', {'context' : context})
    return render(request, 'edit_profile.html')
Reply all
Reply to author
Forward
0 new messages