【问题标题】:Django: How to enable users to delete their accountDjango:如何使用户能够删除他们的帐户
【发布时间】:2020-12-30 16:32:21
【问题描述】:

我不确定出了什么问题,但我仍然无法删除自己的帐户。

我只是被重定向到一个主页。

Views.py

def delete_user(request, username):

    if request.method == 'DELETE':
        try:
            user = User.objects.get(username = username)
            user.delete()
            context['msg'] = 'Bye Bye'
        except Exception as e: 
            context['msg'] = 'Something went wrong!'

    else:
        context['msg'] = 'Request method should be "DELETE"!'

    return render(request, 'HomeFeed/snippets/home.html', context=context) 

urls.py

from account.views import (
    delete_user,
)

 path('delete/<username>', delete_user, name='delete_account'),

account.html

      <form action="{% url 'account:delete_account' username=request.user.username %}" method="GET">
  <a class="mt-4 btn btn-danger deleteaccount" onclick="return confirm('Are you sure you want to delete your account')" href="">Delete Account</a> 
</form>   

【问题讨论】:

  • 在您的第一个视图中,您不需要 kwargs.get(user__id)。只需删除该行
  • 不用那行再试一次
  • 您好,请参考更新后的代码,不行还是不行

标签: python django


【解决方案1】:
  1. 首先如果你想删除登录的用户,你不需要在参数中传递它。刚刚在你的函数中调用它request.user
  2. 我尝试了您的代码,并稍作修改,如下面的代码(仅用于测试)。有用。用户将被删除。粘贴此代码并与邮递员一起尝试。我不知道您的项目,但请确保您的方法 DELETE 请求正常工作。根据 HTML 标准,表单的有效方法是 GET 和 POST。所以你不能这样做&lt;form method="delete"&gt;。但是 Django 正确处理“PUT”和“DELETE”(以及所有其他)http 方法。您可以通过 ajax 进行 PUT 和 DELETE http 调用。

只是为了测试,用DELETE方法调用Postman localhost:8000/delete/user1

from django.contrib import admin
from django.urls import path
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt

# Create your views here.


@csrf_exempt
def delete_user(request, username):
    
    if request.method == 'DELETE':
        try:
            user = User.objects.get(username=username)
            user.delete()
            return HttpResponse('Bye bye')
        except Exception as e: 
            return HttpResponse('Something went wrong!')

    else:
        return HttpResponse('Request method should be "DELETE"!')



urlpatterns = [
    path('admin/', admin.site.urls),
    path('delete/<username>', delete_user)
]

注意:我使用 @csrf_exempt 跳过 CSRF_TOKEN 验证

【讨论】:

【解决方案2】:

您的代码只是停用用户不要删除用户。因此用户在不活动时无法再次登录。

虽然您希望用户删除自己,而不是其他任何人。直接从请求中获取用户。不需要url。

def delete_user(request, username):
    context = {}
    
    if not request.user.is_authenticated:
        return redirect("login")

    if request.method == 'DELETE':
        try:
            user = request.user
            user.delete()
            context['msg'] = 'Bye Bye'
        except Exception as e: 
            context['msg'] = 'Something went wrong!'

    else:
        context['msg'] = 'Request method should be "DELETE"!'

    return render(request, 'template.html', context=context) 

路径应该是这样的

path('account/delete', delete_user, name='delete_account'),

注意:请求应该是DELETE 而不是GETPOST

【讨论】:

  • 嗨 ishak,感谢您的回复。我已经编辑了我的代码以符合您的建议,但没有成功,请参阅帖子并查看我收到的错误
猜你喜欢
  • 2018-01-28
  • 2020-04-23
  • 2016-10-29
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多