这对我有用,我只是想知道这是“安全”还是推荐的做事方式。
def yours(obj, request):
if obj.user == request.user:
return True
else:
return False
@login_required
def classroom_delete(request, pk, template_name='reports/classroom_confirm_delete.html'):
classroom = get_object_or_404(Classroom, pk=pk)
# Is this enough?
if not yours(classroom, request):
HttpResponseRedirect('/')
我将在我的所有删除函数中实现它,删除具有用户外键的对象。
答案 0 :(得分:0)
我最终找到了所有get_object_or_404
个函数,并在没有函数的情况下进行了检查。
classroom = get_object_or_404(Classroom, pk=pk)
if classroom.user != request.user:
return HttpResponseRedirect('/')
因为无论如何我发现我需要做这样的事情:
student = get_object_or_404(Student, pk=pk)
if student.classroom.user != request.user:
return HttpResponseRedirect('/')