如何在django中删除用户?

时间:2015-11-15 02:53:52

标签: python django django-users

这听起来可能是一个愚蠢的问题,但我在使用此视图删除django中的用户时遇到了困难:

@staff_member_required 
def del_user(request, username):    
    try:
        u = User.objects.get(username = username)
        u.delete()
        messages.sucess(request, "The user is deleted")
    except:
      messages.error(request, "The user not found")    
    return render(request, 'front.html')

在urls.py中我有

url(r'^del_user/(?P<username>[\w|\W.-]+)/$', 'profile.views.del_user'), 

相反,用户被删除我获得了The user not found

这里有什么不妥?

1 个答案:

答案 0 :(得分:5)

您应该将代码更改为:

var number = 5000;
var percentX = 12;
var result;

function percentCalculation(a, b){
  var c = (parseFloat(a)*parseFloat(b))/100;
  return parseFloat(c);
}

result = percentCalculation(number, percentX); //calculate percentX% of number

并在模板中显示@staff_member_required def del_user(request, username): try: u = User.objects.get(username = username) u.delete() messages.success(request, "The user is deleted") except User.DoesNotExist: messages.error(request, "User doesnot exist") return render(request, 'front.html') except Exception as e: return render(request, 'front.html',{'err':e.message}) return render(request, 'front.html') 以查看更多错误消息