python - Django-registration: How to allow user delete their account? -
i have simple website users can register in order have access private content , receive newsletter. have used django-registration user registration , authentication, , used html templates here.
the whole system working (login, logout, password recovery, etc.) realized user cannot delete account website. there plugin designed this, or how extend registration classes that? deleting, i'd prefer real suppression, not making user inactive.
you can this:
def delete_user(request, username): context = {} try: u = user.objects.get(username=username) u.delete() context['msg'] = 'the user deleted.' except user.doesnotexist: context['msg'] = 'user not exist.' except exception e: context['msg'] = e.message return render(request, 'template.html', context=context)
and have url pattern like:
url(r'^delete/(?p<username>[\w|\w.-]+)/$', views.delete_user, name='delete-user')
this work. delete user given username.
but, the docs say:
is_active
boolean. designates whether user account should considered active. we recommend set flag
false
instead of deleting accounts; way, if applications have foreign keys users, foreign keys won’t break.
it better approach set user inactive instead of deleting totally database, because foreign keys break.
so can do:
def delete_user(request, username): context = {} try: user = user.object.get(username=username) user.is_active = false user.save() context['msg'] = 'profile disabled.' except user.doesnotexist: # ... except exception e: # ... # ...
and can access view, must add permissions. straightforward approach override built-in @user_passes_test
decorator:
@user_passes_test(lambda u: u.is_staff, login_url=reverse_lazy('login')) def delete_user(request, username): # ...
Comments
Post a Comment