码迷,mamicode.com
首页 > 其他好文 > 详细

django简单用户登陆验证

时间:2016-09-28 20:01:39      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:django简单用户登陆验证


django简单用户登陆验证




django简单用户登陆验证

一.django简单用户登陆验证
  前端页面:
    <div class="container  col-lg-6  col-lg-offset-4">
        <br><br><br><br><br>
      <form class="form-signin col-sm-4 col-lg-offset-2" action="{% url ‘login‘ %}" role="form" method="post"> {% csrf_token %}
        <h2 class="form-signin-heading">Please sign in</h2>
        <input type="text" class="form-control" name="username" placeholder="Username" required="" autofocus="">
        <input type="password" class="form-control" name="password" placeholder="Password" required="">
        <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me"> Remember me
          </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>

    后端验证
    from django.shortcuts import render,HttpResponseRedirect
    from django.contrib.auth import authenticate,login,logout
    from django.contrib.auth.decorators import login_required
    def acc_login(request):
    if request.method == ‘POST‘:
        print request.method
        username = request.POST.get(‘username‘)
        passwd = request.POST.get(‘password‘)
        user = authenticate(username=username,password=passwd)
        print ‘username:%s \n passwd:%s \n user:%s‘ %(username,passwd,user)
        if user is not None:#pass authtencation
            login(request,user)
            return HttpResponseRedirect(‘/‘)
        else:
            return render(request,‘login.html‘,{
                ‘login_err‘:"Wrong username or password!"
            })
    else:
        return render(request,‘login.html‘)
from django.contrib.auth import authenticateuser = authenticate(username=‘john‘, password=‘secret‘)if user is not None:
    # the password verified for the user
    if user.is_active:
        print("User is valid, active and authenticated")
    else:
        print("The password is valid, but the account has been disabled!")else:
    # the authentication system was unable to verify the username and password
    print("The username and password were incorrect.")
    
    来源:http://python.usyiyi.cn/django/intro/tutorial02.html


本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1857353

django简单用户登陆验证

标签:django简单用户登陆验证

原文地址:http://lvnian.blog.51cto.com/7155281/1857353

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!