Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Django problem with files
#1
When working on the test server I am able to delete old user profile images when a new one is uploaded, however when attempting this on a staging server the files do not get deleted. Using apache2 server .Any help is great. Relevant code.
views.py

import os

@login_required
def edit_profile(request):

    if request.method == 'POST':

        if request.POST.get('image') == '':
            pass
        else:
            old_img = request.user.profile.image
            if os.path.exists('media/' + str(old_img)):
                del_img = 'media/' + str(old_img)
                os.remove(del_img)
            else:
                pass
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES,  instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Your profile has been updated.')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form,
    }
    return render(request, 'users/edit_profile.html', context)
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
Johnny's CookBook
Quote
#2
Well this is what I came up with to work. Probably not the best solution but, I'm learning more.
Used the os.path.isfile for testing then took it out.

@login_required
def edit_profile(request):
    curr_img = request.user.profile.image.url # Getting the current image


    if request.method == 'POST':

        if request.POST.get('image') == '': #Checking to see if the user changed image or left blank
            pass
        else:
            os.remove('/path/to/top/level/folder' + str(curr_img)) # What I had to use to get the old file removed


        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES,  instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Your profile has been updated.')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form,
    }
    return render(request, 'users/edit_profile.html', context)
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
Johnny's CookBook
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Django admin login problem erfanakbari1 0 103 Dec-29-2019, 12:29 PM
Last Post: erfanakbari1
  Django Deployment Problem DoktorSoosMann 0 613 Aug-16-2018, 01:42 PM
Last Post: DoktorSoosMann
  first django site-ms word/pdf files jon0852 1 2,307 Nov-19-2017, 08:39 PM
Last Post: homayoon_hashemi
  Django loading static files Dutchpy 7 16,997 Oct-13-2017, 10:25 AM
Last Post: hbknjr
  Django startapp problem nengkya 4 3,405 Jul-01-2017, 08:37 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)