django简单用户登陆验证

django简单用户登陆验证

一.django简单用户登陆验证  前端页面:    
        
      
 {% csrf_token %}        
Please sign in        
        
        
          
                
Sign in          后端验证    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

首页中登录/退出按钮

  {% if  request.user.is_authenticated  %}
    
        
            {
{ request.user.userprofile.name }}            
                
      
  • Logout
  •       {% else %}
      
  • Login
  •   {% endif %}

             {% if  request.user.is_authenticated  %}        
                
                    
                        {
    { request.user.userprofile.name }}                    
                                            
                  
  • Logout
  •                           {% else %}         
              
  • Login
  •             {% endif %}