Django 用户系统 注册 校验 登陆

简单的使用Django做用户注册,登陆等功能的demo
详情推荐阅读:http://python.usyiyi.cn/documents/django_182/topics/auth/default.html#django.contrib.auth.authenticate

Django 中的用户认证
https://www.bbsmax.com/A/xl56ZLP45r/

[Django实战] 第4篇 - 用户认证(用户登录与注销)http://blog.csdn.net/u010415792/article/category/1622583

1.用户注册

#前台提交表单过来,request.POST接收到,然后用User.objects.create_user创建对象,用save保存,注意此处一定要用User.objects.create_user,如果用普通的ORM API的方法存,会导致password以明文存放,以及无法验证通过,因为验证的时候最终要调用identify_hasher()做哈希校验的,巨坑啊一定注意!
def user_register(request):
    username = request.POST['username']
    password = request.POST['password']
    email = request.POST['email']
    user = User.objects.create_user(username=username,password=password,email=email)
    user.save()
    return redirect('pythonnav:index')

2.用户登陆

def user_login(request):
    username = request.POST['username']
    password = request.POST['password']

    user = authenticate(username=username,password=password)
    print 'get user objects %s' %user
    if user is not None:
        # if user.is_active:
        login(request,user)
        print 'succeed'
        return render(request,'index.html')

    else:
        print 'failed'

3.用户退出

def user_logout(request):
    logout(request)
    return redirect('pythonnav:index')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值