I am trying to figure out the best way to allow users to delete or deactivate their account using django-allauth and I'm assuming I need to set is_active field to False.
My solution below has a few issues: 1) The rendered checkbox field is_active shows the text "Designates whether this user should be treated as active. Unselect this instead of deleting accounts." How do I change this text to "Check this box if you are sure you want to delete this account."?
Update: Just realized checking the box sets is_active to True while unchecking the box sets it to False, so perhaps I might need to use a different field to validate the form?
2) How do I logout user on submit?
forms.py:
class DeactivateUserForm(forms.ModelForm):
class Meta:
model = User
fields = ['is_active']
views.py:
login_required(login_url='/accounts/login/')
def deactivate_user_view(request):
pk = request.user.id
user = User.objects.get(pk=pk)
user_form = DeactivateUserForm(instance=user)
if request.user.is_authenticated() and request.user.id == user.id:
if request.method == "POST":
user_form = DeactivateUserForm(request.POST, instance=user)
if user_form.is_valid():
deactivate_user = user_form.save(commit=False)
user.is_active = False
deactivate_user.save()
return render(request, "account/userprofile_del.html", {
"user_form": user_form,
})
else:
raise PermissionDenied
userprofile_del.html:
<h2>Delete your account</h2>
<hr>
<form action="." method="POST" class="padding">{% csrf_token %}
{{ user_form.as_p}}
<button class="btn btn-primary" type="submit" name="action">{% trans "Confirm" %}</button>
</form>