任何人都知道更好的方法在django中编写此登录功能

dot*_*tty 0 python authentication django

干草我想知道是否有人知道更好的方法来做到这一点.

def login_user(request):
    username = request.POST.get('username')
    password = request.POST.get('password')

    user = User.objects.filter(username=username)

    if user:
        user = user[0]
        if user.password == generate_password(password):
            return HttpResponse("password fine")
        else:
            return HttpResponse("password incorrect")
    else:
        return HttpResponse("no user found by that username")
Run Code Online (Sandbox Code Playgroud)

而generate_password函数就是

generate_password(string):
    return hashlib.sha224(str(string)).hexdigest()
Run Code Online (Sandbox Code Playgroud)

任何想法都会很棒.

谢谢

Ghi*_*que 5

为什么不使用Django auth默认视图

  • 如果您想更好地学习Python,请阅读现有的contrib.auth代码.这样做可以用于启发,而不是用于生产代码.http://code.djangoproject.com/browser/django/trunk/django/contrib/auth (4认同)
  • 这是一个你不应该重新发明的轮子 - 充其量你将付出很多努力已经实施的东西; 在最坏的情况下,你会浪费精力并弄错.你为什么不用你的时间想出一个很棒的互联网网站?解决实际问题,而不是想象中的问题. (2认同)