Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django problem with files
#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)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Django problem with files - by menator01 - Jan-05-2020, 03:25 AM
RE: Django problem with files - by menator01 - Jan-05-2020, 04:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Django admin login problem erfanakbari1 0 2,054 Dec-29-2019, 12:29 PM
Last Post: erfanakbari1
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,311 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django Deployment Problem DoktorSoosMann 0 1,944 Aug-16-2018, 01:42 PM
Last Post: DoktorSoosMann
  first django site-ms word/pdf files jon0852 1 4,378 Nov-19-2017, 08:39 PM
Last Post: homayoon_hashemi
  Django loading static files Dutchpy 7 23,945 Oct-13-2017, 10:25 AM
Last Post: hbknjr
  Django startapp problem nengkya 4 8,048 Jul-01-2017, 08:37 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020