How to allow user to delete their own profile after login with their default id.

669 views
Skip to first unread message

Manjusha

unread,
Dec 24, 2022, 9:13:09 AM12/24/22
to Django users
Hi, I have created authentication system in Django. Anyone just tell me how to allow user to delete the created profile with default id of the user.Screenshot 2022-12-24 193133.png. I I am using built-in  User authentication libraries for this purpose and haven't created database model for the user. But the function (the commented function under delete_account) that I have created it is not deleting the user.
Now I am trying to delete user profile using generic editing views  of Django.
Anyone tell me how to allow user to delete the created profile with default id of the user.

Ryan Nowakowski

unread,
Dec 24, 2022, 10:15:05 AM12/24/22
to django...@googlegroups.com
You're using a class-based view here but your code looks like it's supposed to be for a function based view. Which style do you want to use?

Manjusha

unread,
Dec 24, 2022, 10:21:56 AM12/24/22
to Django users
actually the code I have written it is for function based view but it not working that's why I am trying to do with generic editing views sorry for that 

Manjusha

unread,
Dec 24, 2022, 10:22:32 AM12/24/22
to Django users
that incorrect picture

Manjusha

unread,
Dec 24, 2022, 10:23:33 AM12/24/22
to Django users
want to know how to allow user to delete the created profile with default id of the user.

rahul sharma

unread,
Dec 24, 2022, 11:45:22 AM12/24/22
to django...@googlegroups.com
Delete_status=0
Add coloum in database table 
Like delete_status

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/45912e33-592c-4ca2-b618-c8fb9a1b6066n%40googlegroups.com.

Ryan Nowakowski

unread,
Dec 24, 2022, 12:21:07 PM12/24/22
to django...@googlegroups.com
Well it looks like you have a good start. What, in particular, isn't working? It would help if you can post the corrected code and any error messages/tracebacks.

Eng Thanoon

unread,
Dec 24, 2022, 6:14:06 PM12/24/22
to Django users
***************************************************
views.py
from rest_framework import generics,permissions
class NotifyDestroy(generics.DestroyAPIView):
    """delete current user """
    permission_classes=[permissions.IsAuthenticated]
    serializer_class=user_delete_ser
    queryset=User.objects.all()  

*********************************************
serializers.py

from rest_framework.serializers import ModelSerializer
class user_setting_ser(ModelSerializer):
    class Meta:
        model=User
        fields=['id']


****************************************
urls.py
path('delete_user/<int:pk>',NotifyDestroy.as_view(),name='delete-current-user'), 

*********************************
Message has been deleted
Message has been deleted

Ryan Nowakowski

unread,
Dec 25, 2022, 1:35:21 PM12/25/22
to django...@googlegroups.com
Use the delete method on the instance instead of the class. So replace:
User.delete(u.id)

...with:
u.delete()


On December 25, 2022 1:20:12 AM CST, Manjusha <manjushak...@gmail.com> wrote:
def delete_account(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get('pswd1')
       
        u = User.objects.filter(email=email).first()
        print(u)  
        User.delete(u.id)
        messages.success(request,"Account is deleted successfully.....")
        return redirect('home')
       
    return render(request,"account/delete_profile.html")                                                                                                                                                                                                                                                   by using this function i am trying to delete the user , but it shows me following error i.e attribute error so how can I find default id of the user which is already created at the time of registration, I am not using rest framework of django and also not  created any user model     in model.py file.                                            error.png        In this way I'am registering the user                                                                                                                            def register(response):
    if response.method == "POST" :
        username = response.POST["username"]
        email = response.POST["email"]
        password1 = response.POST["pswd1"]
        password2 = response.POST["pswd2"]

        try:    
            myuser = User.objects.create_user(username, email, password1)
            myuser.password2 = password2
       
            myuser.save()
            #if myuser.objects.filter :
            if len(username) >10:
                messages.error(response,"Username should contain at most 10 characters.")
        except IntegrityError:
            pass
        messages.success(response,"You have successfully registered.")
        return redirect('login')
     
    return render(response,'account/registration.html')                       
Message has been deleted

Eng Thanoon

unread,
Dec 25, 2022, 1:52:40 PM12/25/22
to Django users
from django.contrib.auth import  authenticate
def delete_account(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get('pswd1')
       
        u = User.authenticate(request=request,email=email,password=password) #this will check if any user exist with current password and email .
        if u :
            print(u)  
            u.delete()
            messages.success(request,"Account is deleted successfully.....")
            
            return redirect('home')
        message.error(request,'incorrect user input data')
        return redirect('home')
Reply to all
Reply to the author
Forward


Manjusha

unread,
Dec 25, 2022, 3:27:15 PM12/25/22
to Django users
Thanks, both techniques are working now i.e 1)Replacing User.delete(u.id) with u.delete() and in 2) user is no getting authenticated with email, it is authenticated with only username and password with  authenticate function.                                                                                                                                                                                                                                                                             But it is fine.

Raj Mandaviya

unread,
Dec 26, 2022, 9:40:35 AM12/26/22
to Django users
Hey, 

Here is what you can do:
When User clicks the Delete profile button you use Javascript Confirm then if they press OK you go to the view.

def delete_account(request):
    user = User.objects.get(id=request.user.id)
    # You first logout the user then delete the account
    logout(request)
    # You can use either of these
    # To Soft Delete
    user.active = False
    user.save()
    # To Hard Delete
    user.delete()
    messages.success("You account has been deleted Successfully!")
    return redirect('home')


Let me know if this doesn't work.
-Raj Mandaviya

Manjusha

unread,
Dec 27, 2022, 1:02:11 PM12/27/22
to Django users
User object doesn't have attribute id so it will not work, I have checked now and you can refer this documentation, so you will get clear idea.

Manjusha

unread,
Dec 27, 2022, 1:17:31 PM12/27/22
to Django users
need to create database for user and need to add id column in database.
Reply all
Reply to author
Forward
0 new messages