• django 用户认证/Excel导入Mysql


    用户认证:
    #用户登录测试
    from django.contrib.auth.decorators import login_required
    from django.contrib import auth
    

     在Views加入以下代码:

    #这里登录
    def account_login(request):
        username = request.POST.get('username')
        password = request.POST.get('password')
    	#登录
        user = auth.authenticate(username=username,password=password)
    	#如果登录不为空
        if user is not None:
            auth.login(request,user)
    	    #转到特定页面
            return HttpResponseRedirect('/ipinfo/')
    	else:
            return render_to_response('index.html',{'err':'wrong username or password!'},context_instance=RequestContext(request))
    		
    		
    #退出的页面
    def logout(request):
    	#退出登录
        auth.logout(request)
        return  HttpResponseRedirect('/')
    	
    	
    #如果想要有所有页面上启用登录检测,未登录就不允许访问,直接访问登录页面
    #在每个方法前加这个修饰函数
    @login_required 
    def ipinfo(request):
    	XXXXXX
    

     

    EXCEL导入Mysql:

    #!/usr/env python
    #coding=utf-8
    import xlrd
    import pymysql
    import time
    
    xls = xlrd.open_workbook('d:/book.xls')
    sheets = xls.sheet_names()
    table = xls.sheets()[0]
    rows = table.nrows
    
    myconn = pymysql.connect('127.0.0.1','root','111111','mydb',charset='utf8')
    mycur = myconn.cursor()
    sql = 'INSERT INTO ipaddr VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
    for line in range(1,rows):
    	each_line = table.row_values(line)
    	mycur.execute(sql,each_line)
    mycur.close()
    myconn.commit()
    myconn.close()
    

      

     

  • 相关阅读:
    bzoj3675 [Apio2014]序列分割
    bzoj3437 小P的牧场
    bzoj3156 防御准备
    bzoj1911 [Apio2010]特别行动队
    Codeforces 937.D Sleepy Game
    Codeforces 937.C Save Energy!
    Codeforces 937.B Vile Grasshoppers
    bzoj1597 [Usaco2008 Mar]土地购买
    DDA画线算法
    实验九
  • 原文地址:https://www.cnblogs.com/syother/p/6732804.html
Copyright © 2020-2023  润新知