gpt4 book ai didi

python - 基于django类的删除 View 和验证

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:55 25 4
gpt4 key购买 nike

我的看法:

class ModEmailDeleteView(DetailView):
model = EmailModel
template_name = "email_delete.html"
success_url = reverse_lazy('moderator_profile', request.user.id)

这里我想检查特定字段的用户是否是请求用户然后只有他可以删除。喜欢

if obj.user == request.user

那么只有他可以删除或者抛出404

如果我在 url 中有多个主键怎么办?并希望使用这些主键进行不同的验证

最佳答案

你可以这样做:

class ModEmailDeleteView(DeleteView):
model = EmailModel
template_name = "email_delete.html"
success_url = reverse_lazy('moderator_profile', request.user.id)

def delete(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.user == request.user:
self.object.delete()
return HttpResponseRedirect(self.get_success_url())
else:
raise Http404 #or return HttpResponse('404_url')

关于python - 基于django类的删除 View 和验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com