用户认证:
#用户登录测试 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()