[Solved]-How to delete user in django?

17👍

You should change your code to:

@staff_member_required 
def del_user(request, username):    
    try:
        u = User.objects.get(username = username)
        u.delete()
        messages.success(request, "The user is deleted")            

    except User.DoesNotExist:
        messages.error(request, "User doesnot exist")    
        return render(request, 'front.html')

    except Exception as e: 
        return render(request, 'front.html',{'err':e.message})

    return render(request, 'front.html') 

and display the err in your template to see further error messages

-5👍

@login_required
def del_user(request):
    user = request.user
    user.delete()
    redirect("login")

Leave a comment