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 :
u.delete()
messages.success(request,"Account is deleted successfully.....")
print(u)
return redirect('home')
message.error(request,'incorrect user input data')
return redirect('home')
On Sunday, 25 December 2022 at 21:35:21 UTC+3 Ryan Nowakowski wrote:
> 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 <[email protected]>
> 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. [image:
>> 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')
>>
>> On Sunday, December 25, 2022 at 4:44:06 AM UTC+5:30 [email protected]
>> wrote:
>>
>>> ***************************************************
>>> 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'),
>>>
>>>
>>>> *********************************
>>>
>>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/17b887b5-8d0f-49f8-bbe4-e84592fcf1a1n%40googlegroups.com.