79455987

Date: 2025-02-20 22:47:16
Score: 1.5
Natty:
Report link

Error was in the view due to not handling the initial invalid form when this code was first called. Thanks to @willeM_VanOnsem for pointing out irregularities which allowed me to find the issue and correct.

@login_required(login_url="/login")
def user_profile_admin(request):

    if request.method == "POST":
        user_id = request.POST.get("user_id")
        if user_id != None:  # this is when edit button pressed in members.html
            user_object = User.objects.get(pk=user_id)

        email = request.POST.get("email")
        if email != None:  # this is when the user admin form is actually submitted
            user_object = User.objects.get(email=email)
    
        athletes = user_object.athletes.all() #athletes for the user being updated

        form = UserAdminForm(request.POST  or None, instance=user_object) 

        if form.is_valid():
            user_object = form.save()
            success_message="You have updated profile for user : "+email +" successfully"
            messages.success(request, success_message)
            return redirect('members')
        else:
            print(form.errors.as_data()) # here you print errors to terminal
            user_id = request.POST.get("user_id")
            if user_id != None:  # this is when edit button pressed in members.html
                user_object = User.objects.get(pk=user_id)
                print("user id")

            email = request.POST.get("email")
            if email != None:  # this is when the user admin form is actually submitted
                user_object = User.objects.get(email=email)
                print("email")

            form = UserAdminForm(instance=user_object) 
            return render(request, 'account/user_profile_admin_form.html', {"form": form, "athletes": athletes}) 

    else:
        return render(request, 'members') 
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @willeM_VanOnsem
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: SteveM