def myhome_doregister(request): try: # 接收表单注册的数据 data = request.POST.dict() data.pop('csrfmiddlewaretoken') # 验证手机短信验证码是否正确 msgcode = request.session['msgcode'] if data['vcode'] == msgcode['code'] and data['phone'] == msgcode['phone']: # 删除表单中的 vcode data.pop('vcode') # # 验证手机号是否存在 # res = Users.objects.filter(phone=data['phone']).count() # if res: # # 手机号已存在 # return HttpResponse('<script>alert("手机号已存在");history.back(-1);</script>') # 密码进行加密处理 data['password'] = make_password(data['password'], None, 'pbkdf2_sha256') # 执行注册 ,数据的添加 ob = Users(**data) ob.save() return HttpResponse('<script>alert("注册成功,请登录");location.href="/login/";</script>') else: return HttpResponse('<script>alert("手机验证码错误");history.back(-1);</script>') except: pass return HttpResponse('<script>alert("注册失败,请联系管理员");location.href="/register/";</script>')
def registuser(request): # return HttpResponse(request.POST['password']) userlist = Users.objects.all() for u in userlist: if request.POST['username'] == u.username: context = {'info': '用户名已存在'} return render(request, "myweb/register.html", context) if request.POST['password'] == request.POST[ 'password2'] and request.POST['password'] != '': ob = Users() ob.username = request.POST['username'] # ob.name = request.POST['name'] #获取密码并md5 m = hashlib.md5() m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() # ob.sex = request.POST['sex'] # ob.code = request.POST['code'] # ob.phone = request.POST['phone'] # ob.email = request.POST['email'] # ob.status = request.POST['status'] # ob.addtime = time.time() ob.save() context = {'info': '注册成功!'} return render(request, "myweb/info.html", context) else: context = {'info': '两次密码不一致'} return render(request, "myweb/register.html", context)
def doregister(request): aa = request.POST['username'] bb = request.POST['password'] if not aa: return HttpResponse( '<script>alert("请输入用户名");location.href="/register/"</script>') if not bb: return HttpResponse( '<script>alert("请输入密码");location.href="/register/"</script>') else: # 判断用户名是否存在 res = Users.objects.filter(username=request.POST['username']).exists() if res: return HttpResponse( '<script>alert("用户名已经存在");location.href="/register/"</script>') else: ob = Users() ob.username = request.POST['username'] ob.password = make_password(request.POST['password'], None, 'pbkdf2_sha256') ob.save() request.session['VipUser'] = { 'name': ob.username, 'pic': ob.picurl, 'uid': ob.id } return HttpResponse( '<script>alert("注册成功");location.href="/"</script>')
def zhucego(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] if request.POST['password'] == request.POST['repassword']: #获取密码并md5 import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.address = request.POST['address'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = 1 ob.addtime = time.time() ob.save() context = {'info': '添加成功!'} else: context = {'info': '密码不一致'} except: context = {'info': '添加失败!'} #return render(request,"myadmin/info.html",context) return render(request, "myweb/info.html", context)
def users_insert(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] if request.POST['password'] == request.POST['repassword']: #获取密码并m5d import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.address = request.POST['address'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = 1 ob.addtime = time.time() ob.save() context = {'info': '添加成功!'} else: return redirect(reverse('myadmin_uadd')) except: context = {'info': '添加失败'} return render(request, 'myadmin/info.html', context)
def usersinsert(request): """ 添加会员信息 :param request: """ try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] # 获取密码, 用md5加密 m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.address = request.POST['address'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = 1 ob.save() context = {'info': '修改成功! '} except: context = {'info': '修改失败! '} return render(request, 'myadmin/info.html', context)
def doregister(request): try: user = Users.objects.all() if user.filter(username=request.POST['username']): context = {'info': '此用户名已经注册!'} else: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() if request.POST['password'] == request.POST['repassword']: ob.address = request.POST['address'] ob.email = request.POST['email'] ob.phone = request.POST['phone'] ob.code = request.POST['code'] ob.addtime = time.time() ob.save() return render(request, "myweb/login.html") else: context = {'info': '请重新确认密码!'} return render(request, "myweb/register.html", context) except: context = {'info': '注册失败啦!'} return render(request, "myweb/info.html", context)
def myhome_doregister(request): try: # 接收表单数据 data = request.POST.dict() data.pop('csrfmiddlewaretoken') data.pop('againpassword') # 验证手机验证码是否正确 if data['vcode'] == request.session['msgcode']['code'] and data['phone'] == request.session['msgcode']['phone']: data.pop('vcode') # 验证手机号是否存在 res = Users.objects.filter(phone=data['phone']).count() # 手机号已存在 if res: return HttpResponse('<script>alert("该手机号已被注册");history.back(-1)</script>') data['password'] = make_password(data['password'],None,'pbkdf2_sha256') ob = Users(**data) ob.save() return HttpResponse('<script>alert("注册成功,请登陆");location.href="/login/";</script>') else: return HttpResponse('<script>alert("手机验证码错误");history.back(-1)</script>') except: pass return HttpResponse('<script>alert("注册失败,请联系相关工作人员");history.back(-1)</script>')
def register(request): #判断当前请求方式 if request.method == 'GET': return render(request, 'home/register.html') elif request.method == 'POST': #执行注册 # 判断用户名是否存在 res = Users.objects.filter(username=request.POST['username']).exists() if res: return HttpResponse( '<script>alert("用户名已存在");location.href="/register"</script>') else: ob = Users() ob.username = request.POST['username'] ob.password = make_password(request.POST['password'], None, 'pbkdf2_sha256') #把用户信息存储到session request.session['VipUser'] = { 'name': ob.username, 'pic': ob.picurl, 'uid': ob.id } return HttpResponse( '<script>alert("注册成功,欢迎登录");location.href="/"</script>')
def do_register(request): if request.POST['password'] != request.POST['repassword']: return JsonResponse({'msg': 'passwd_error'}) elif Users.objects.filter(username=request.POST['username']): return JsonResponse({'msg': 'account_error'}) else: users = Users() users.username = request.POST['username'] md5 = hashlib.md5() md5.update(bytes(request.POST['password'], encoding='UTF-8')) # 十六进制保存到数据库 users.password = md5.hexdigest() users.name = request.POST['name'] users.mobile = request.POST['mobile'] users.address = request.POST['address'] users.save() return JsonResponse({'msg': 'success'})
def user_insert(request): from django.contrib.auth.hashers import make_password # list1 = ['username', 'password', 'phone', 'email', 'address', 'age', 'sex', 'status'] res = request.POST try: uname = res['username'] user_a = Users() user_a.username = res['username'] user_a.password = make_password(res['password'], None, 'pbkdf2_sha256') user_a.phone = res['phone'] user_a.email = res['email'] user_a.address = res['address'] user_a.age = res['age'] if res['sex'] in ('0', '1'): user_a.sex = res['sex'] else: a = '\033[1;31mERROR: \033[1;34m文件:%s\033[1;31m 年龄字段 插入 error , 插入价值不是男或女\033[0m' % ( MYADMAIN_USER_V1) print(a) raise AttributeError("年龄字段 插入 error , 插入价值不是男或女, %s" % a) if res['status'] in ('0', '1', '2'): user_a.status = res['status'] else: b = '\033[1;31mERROR: \033[1;34m文件:%s\033[1;31m 状态字段 插入 error, 插入值不正确\033[0m' % ( MYADMAIN_USER_V1) print(b) raise AttributeError("状态字段 插入 error, 插入值不正确, %s" % b) # user_a.status = res['status'] if request.FILES.get('pic', None): user_a.pic = filesload(request, uname) user_a.save() return HttpResponse( '<script>alert("添加成功");location.href="/myadmin/user_index"</script>' ) except AttributeError as arterror: return HttpResponse( '<script>alert("添加失败, 图片上传错误, 请重新添加");location.href="/myadmin/useradd"</script>' ) except BaseException as be: return HttpResponse( '<script>alert("添加失败, 字段指定错误请重新制定");location.href="/myadmin/useradd"</script>' )
def post(self, request): try: from django.contrib.auth.hashers import make_password res = request.POST list1 = [ 'username', 'password', 'phone', 'email', 'address', 'age', 'sex' ] for i in list1: if not res.get(i): print(res.get(i)) return HttpResponse( '<script>alert("字段为空, 无法添加");location.href="/register/"</script>' ) print('username_res', res.get('username')) users_name = Users.objects.filter(username=res.get('username')) print(users_name) if len(users_name) != 0: return HttpResponse( '<script>alert("用户名已存在, 请重新添加");location.href="/register/"</script>' ) user_a = Users() user_a.username = res['username'] user_a.password = make_password(res['password'], None, 'pbkdf2_sha256') user_a.phone = res['phone'] user_a.email = res['email'] user_a.address = res['address'] user_a.age = res['age'] user_a.sex = res['sex'] user_a.status = 1 user_a.save() request.session['login_status'] = True request.session['login_users'] = { 'user_id': user_a.id, 'username': user_a.username } except: return HttpResponse( '<script>alert("表单信息不正确, 请重新添加");location.href="/register/"</script>' ) return redirect(reverse('home_index'))
def git_check(request): import time type='1' request_code = request.GET.get('code') print(request_code) oauth_git = OAuth_GITHUB(settings.GITHUB_APP_ID,settings.GITHUB_KEY,settings.GITHUB_CALLBACK_URL) try: access_token = oauth_git.get_access_token(request_code) #获取access token time.sleep(0.1) #此处需要休息一下,避免发送urlopen的10060错误 except: #获取令牌失败,反馈失败信息 return HttpResponse('<script>alert("授权失败,返回登录页面");location.href="/login/";</script>') infos = oauth_git.get_user_info() #获取用户信息 nickname = infos.get('login','') image_url = infos.get('avatar_url','') open_id = str(oauth_git.openid) sex = '1' filename = '' + image_url.split('/')[-1] filename = filename.split('?')[0] filename = filename + '.jpg' pic_url = '/static/uploads/userpic/' + filename githubs = OAuth_ex.objects.filter(openid=open_id,type=type) #查询是否该第三方账户已绑定本网站账号 if githubs: #若已绑定,直接登录 oauth_init = OAuth_ex.objects.get(openid=open_id,type=type) ob = Users.objects.get(id = oauth_init.user.id) request.session['VipUser'] = {'uid': ob.id, 'nikename': ob.nikename, 'pic_url': ob.pic_url} return render(request,'myhome/index.html') email = oauth_git.get_email() users = Users.objects.filter(email=email) #若获取到邮箱,则查询是否存在本站用户 if users: #若存在,则直接绑定 user = users[0] else: #若不存在,则新建本站用户 while Users.objects.filter(nikename=nickname): #防止用户名重复 nickname = nickname + '*' password = '******' user = Users(nikename=nickname,email=email,sex=sex,password=make_password(password, None, 'pbkdf2_sha256'),pic_url=pic_url) down_picture(image_url) #下载用户头像图片 user.save() oauth_ex = OAuth_ex(user = user,openid = open_id,type=type) oauth_ex.save() #保存后登陆 oauth_init = OAuth_ex.objects.get(openid=open_id, type=type) ob = Users.objects.get(id=oauth_init.user.id) request.session['VipUser'] = {'uid': ob.id, 'nikename': nickname, 'pic_url': ob.pic_url} return render(request,'myhome/index.html')
def insertuser(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] #获取密码并md5 m = hashlib.md5() m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.status = request.POST['status'] ob.addtime = time.time() ob.save() context = {'info': '添加成功!'} except: context = {'info': '添加失败!'} return render(request, "myadmin/info.html", context)
def usersinsert(request): try: a=Users() a.username=request.POST["username"] a.name=request.POST['name'] import hashlib m=hashlib.md5() m.update(bytes(request.POST['password'],encoding='utf8')) a.password=m.hexdigest() a.sex=request.POST['sex'] a.address=request.POST['address'] a.code=request.POST['code'] a.phone=request.POST['phone'] a.email=request.POST['email'] a.state=request.POST['state'] a.addtime=time.time() a.save() context={'info':'添加成功'} except: context={'info':'添加失败'} return render(request,'myadmin/info.html',context)
def adminInsert(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.adress = request.POST['adress'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = request.POST['state'] ob.addtime = request.POST['addtime'] ob.save() context = {'info': '添加成功'} except: context = {'info': '添加失败'} return render(request, "myadmin/users/info.html", context)
def usersinsert(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] # 获取密码并md5 import hashlib m = hashlib.md5() # update函数要求参数是bytes m.update(bytes(request.POST['password'], encoding="utf8")) ob.password = m.hexdigest() ob.gender = request.POST['sex'] ob.address = request.POST['address'] ob.state = 1 ob.save() context = {"info":"添加成功"} except: context = {"info":"添加失败"} return render(request, "myadmin/info.html", context)
def insertregedit(request): try: ob = Users() ob.username = request.POST.get('name') from django.contrib.auth.hashers import make_password ob.password = make_password(request.POST.get('password'), None, 'pbkdf2_sha256') aa = request.POST.get('password2') ob.email = request.POST.get('email') ob.state = request.POST.get('state') if not request.FILES.get('img'): ob.img = 'static/public/img/9110.jpg' else: ob.img = request.FILES.get('img') ob.save() return HttpResponse( "<script>alert('注册成功');location.href='/user/index'</script>") except: return HttpResponse( "<script>alert('注册失败');location.href='/user/index'</script>")
def do_useradd(request): # 添加try的原因: # 防止发生内部错误:例如外键冲突,字段唯一性等 try: us = Users() us.username = request.POST['username'] # 实例化md5实例 md5 = hashlib.md5() # python3中md5只能加密字节型数据,所以要用bytes强制转换 md5.update(bytes(request.POST['password'],encoding="utf-8")) us.password = md5.hexdigest() us.name = request.POST['name'] us.gender = request.POST['sex'] us.mobile = request.POST['mobile'] us.address = request.POST['address'] us.state = 0 us.status = 0 us.save() context = {"info":"添加成功"} except: context = {"info": "添加失败"} return render(request, "myadmin/info.html", context)
def register(request): # 判断当前请求的方式 if request.method == 'GET': return render(request, 'home/register.html') elif request.method == 'POST': # 执行注册 # 判断用户是否存在 res = Users.objects.filter(username=request.POST['username']).exists() if res: return HttpResponse( '<script>alert("用户名已存在");location.href="/register"</script>') # 把用户信息存储到session else: ob = Users() print(ob) ob.username = request.POST['username'] ob.password = make_password(request.POST['password'], None, 'pbkdf2_sha256') ob.save() return HttpResponse( '<script>alert("注册成功");location.href="/login/"</script>')
def zcinsert(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] # 进行加密 import hashlib m = hashlib.md5() m.update(bytes(request.POST['passwd'], encoding="utf8")) ob.passwd = m.hexdigest() ob.sex = request.POST['sex'] ob.address = request.POST['address'] ob.code = 220077 ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = 1 ob.addtime = time.time() ob.save() context = {'info': '添加成功'} except: context = {'info': '添加失败'} return render(request, 'myadmin/info.html', context)
def usersinsert(request): try: ob = Users() ob.username = request.POST['username'] ob.name = request.POST['name'] #密码md5加密 import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) ob.password = m.hexdigest() ob.sex = request.POST['sex'] ob.address = request.POST['address'] ob.code = request.POST['code'] ob.phone = request.POST['phone'] ob.email = request.POST['email'] ob.state = 1 ob.addtime = time.time() #保存进数据库 ob.save() context = {'info': '添加成功!'} except: context = {'info': '添加失败!'} return render(request, 'myadmin/info.html', context)