def login_page(request): form = UserForm(request.GET or None) if form.is_valid() and form.clean(): session_key = get_random_string(32, VALID_KEY_CHARS) while Session.objects.filter(sessionid=session_key): session_key = get_random_string(32, VALID_KEY_CHARS) name = request.GET['username'] if user.objects.filter(username = str(name))[0].type == 'admin': response = HttpResponseRedirect('../admin/home/') else: response = HttpResponseRedirect('../home/') response.set_cookie('sessionid', session_key) Session.objects.create(sessionid=session_key, username = name) return response return render_to_response('login/login.html', locals(), RequestContext(request))
def userdetail(request, uid): #uid是用户的id user = User.objects.get(pk=uid) useraddr = user.useraddr_set.filter(isdefault=0).first() form = UserForm() if request.method == 'POST': form = UserForm(request.POST) print(request.POST) if form.is_valid(): print('-' * 80) file = request.FILES.get('photo') print(file) print('+' * 70) if file: path = os.path.join(settings.MEDIA_ROOT, file.name) # list = path.split('/') # print(path.split('/')) # path = '/'+ list[-3]+'/'+list[-2]+'/'+list[-1] print(path) print('+' * 70) # # 文件类型过滤 # ext = os.path.splitext(file.name) # print(ext) # if len(ext) < 1 or not ext[1] in settings.ALLOWED_FILEEXTS: # return redirect(reverse('admin:userdetail',kwargs={'uid':uid})) # 解决文件重名 # if os.path.exists(path): # # 日期目录 # dir = datetime.today().strftime("%Y/%m/%d") # dir = os.path.join(settings.MEDIA_ROOT, dir) # if not os.path.exists(dir): # os.makedirs(dir) # 递归创建目录 # # list.png # file_name = ext[0] + datetime.today().strftime("%Y%m%d%H%M%S") + str(randint(1, 1000)) + ext[1] if len(ext) > 1 else '' # path = os.path.join(dir, file_name) # print(path) with open(path, 'wb') as fp: # 如果文件超过2.5M,则分块读写 if file.multiple_chunks(): for block1 in file.chunks(): fp.write(block1) else: fp.write(file.read()) print(path) user.picture = '/' + path user.username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') password = hashlib.sha1(password.encode('utf8')).hexdigest() user.password = password user.ugrade = form.cleaned_data.get('ugrade') user.email = form.cleaned_data.get('email') user.phone = form.cleaned_data.get('phone') user.save() useraddr.province = form.cleaned_data.get('province') # print(useraddr.pro) useraddr.city = form.cleaned_data.get('city') useraddr.district = form.cleaned_data.get('district') useraddr.address = form.cleaned_data.get('address') useraddr.save() user = User.objects.get(pk=uid) useraddr = user.useraddr_set.filter(isdefault=0).first() return render(request, 'admin/user_detail.html', context={ 'user': user, 'useraddr': useraddr }) return render(request, 'admin/user_detail.html', context={ 'user': user, 'useraddr': useraddr, 'form': form })