django实现用户登陆验证源码
admin
2023-08-03 15:20:09
0

今天小编就为大家带来一篇有关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 %}          


    看完上述内容,你们掌握django实现用户登陆验证的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

    相关内容

    热门资讯

    霸占车位的体育局干部彭某曦,她... 就在刚刚,后台很多读者给我留言,说我昨天的文章写得很好呀,怎么不见了?对此我想说的是,我也不知道为什...
    全程锁定 看人工智能如何赋能台... 台风“巴威”持续逼近我国东南沿海。在台风生成之初,上海市气象局的人工智能气象模型就已全程锁定、动态追...
    脉流科技获WIPO全球奖 本报讯 (记者 张苗 李洁薇 实习生 金玉鑫) 当地时间7月10日,在瑞士日内瓦举行的2026年联合...
    山东济宁:让机器人“跑”进千行... 盛夏时节,孔孟之乡迎来一场科技与产业深度交融的大会。7月11日至13日,由山东省机器人行业协会、中国...
    外交部亚洲司负责人就日方涉“南... 2026年7月12日,外交部亚洲司负责人就日本外相炒作“南海仲裁案裁决”出台十年发表谈话、日伙同他国...
    全球首款可变形个人机器人,上纬... IT之家 7 月 12 日消息,上纬新材今日发布启元 T1 实机视频,号称“全球首款可变形个人机器人...
    腾讯重仓一个IPO 图片来源:视觉中国 作者/杨继云 报道/投资界PEdaily 深圳超级IPO来了。 日前,深圳云豹智...
    乌方称“俄军9成导弹和无人机有... 近日,乌克兰总统顾问弗拉修克在接受日本共同社的采访时声称,俄军在进攻乌克兰时使用的巡航和弹道导弹及无...
    美国驻阿曼使馆发布“就地避险”... 当地时间7月12日,美国驻阿曼大使馆向阿曼境内部分地区发布“就地避险”提醒。美国驻阿曼使馆表示,鉴于...
    风雨里的守望——广西防汛抗洪一... ↑ 7月11日,在通往广西南宁横州市镇龙乡的道路上,救援人员徒步向受困地区进发(无人机照片)。近期,...