简单的使用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')