def regist(request): if request.method == "POST": userform = UserForm(request.POST) if userform.is_valid(): username = userform.cleaned_data['username'] password = userform.cleaned_data['password'] email = userform.cleaned_data['email'] userResult = User(username=username, password=password, email=email) userResult.save() return HttpResponse('regist success!!!') else: userform = UserForm() return render(request, 'regist.html', {'userform': userform})
def do_add_user(request): """新增用户(上传图片)""" # todo: 获取用户上传的图片 # django.core.files.uploadedfile.InMemoryUploadedFile upload_file = request.FILES.get('avatar') print(type(upload_file), upload_file.name) # 定义头像保存的路径 filename = upload_file.name # 文件名 file_path = settings.MEDIA_ROOT + '/app01/' + filename # todo: 保存图片到服务器硬盘中 with open(file_path, 'wb') as file: # 遍历多次,把内容写到文件 for data in upload_file.chunks(): file.write(data) # 数据库表新增一个用户(保存用户头像路径) user = User() user.name = request.POST.get('name') user.avatar = '/app01/' + filename user.save() return HttpResponse('新增用户成功')
def do_add_user(request): '''新增用户操作''' # 获取上传的图片对象 upload_file = request.FILES.get('avatar') upload_file = request.FILES.get('avatar') # 判断头像是否为空 if not upload_file: return render(request, 'add_user.html', {'errmsg': '请选择用户头像'}) file_name = upload_file.name file_path = settings.MEDIA_ROOT + "/app01/" + file_name # 将图片保存到服务器中 with open(file_path, 'wb') as file: for data in upload_file.chunks(): file.write(data) u = User() u.name = request.POST.get('name') u.avatar = '/app01/' + file_name u.save() return HttpResponse("新增用户成功")
def zhuce(request): if request.method == "GET": fm = Zhuce() elif request.method == "POST": fm = Zhuce() # 获取值 data = request.POST username = data.get("username") password = data.get("password") #实例化数据库 user = User() user.username = username user.password = password user.save() return render(request, "index.html", locals()) return render(request, "zhuce.html", locals())
def do_add_user(request): # todo: 获取上传的头像 upload_file = request.FILES.get('avatar') # 判空 if not upload_file: return render(request, 'add_user.html', {'errmsg': '请选择用户头像'}) # todo: 保存用户的头像到硬盘中 file_name = upload_file.name file_path = settings.MEDIA_ROOT + '/app01/' + file_name with open(file_path, 'wb') as file: for data in upload_file.chunks(): file.write(data) # todo: 新增一个用户:保存用户名和用户上传的头像的路径到数据库中 user = User() user.username = request.POST.get('username') user.avatar = 'app01/' + file_name user.save() return HttpResponse('新增成功')
def active_user(request, token): if request.method == 'GET': result = User.check_activate_token(token) return HttpResponse(result)
def register(request): data = models.User.objects.all() data1 = models.service_type.objects.all() if request.GET: print(request.GET) username = request.GET['user'] address = request.GET['address'] service_type = request.GET['service'] updatetime = time.strftime('%Y-%m-%d %H:%M:%S') git_svn = request.GET['sex'] if address: if git_svn == "git": print("-------------------------git------------------------") os.system("cd static/upload && git clone %s" % address) dirname = address.split("/")[-1].split(".")[0] print(dirname) shutil.make_archive("static/upload/%s" % dirname, 'gztar', root_dir='static/upload/%s' % dirname) user = User() user.username = username user.headImg = '%s.tar.gz' % dirname user.service_type = service_type user.updatetime = updatetime user.save() return render(request, "register.html", { "get": "成功拉取代码文件到服务端", "data": data, "data1": data1 }) elif git_svn == 'svn': print("-------------------------svn------------------------") svnuser = request.GET['svnuser'] svnpass = request.GET['svnpass'] os.system( "cd static/upload && svn checkout %s --username=%s --password=%s" % (address, svnuser, svnpass)) dirname = address.split("/")[-1] print(dirname) shutil.make_archive("static/upload/%s" % dirname, 'gztar', root_dir='static/upload/%s' % dirname) user = User() user.username = username user.headImg = '%s.tar.gz' % dirname user.service_type = service_type user.updatetime = updatetime user.save() return render(request, "register.html", { "get1": "成功拉取代码文件到服务端", "data": data, "data1": data1 }) if request.method == "POST": uf = UserForm(request.POST, request.FILES) if uf.is_valid(): # 获取表单信息 username = uf.cleaned_data['用户'] headImg = uf.cleaned_data['文件'] service_type = request.POST['service'] updatetime = time.strftime('%Y-%m-%d %H:%M:%S') # print(headImg) # 写入数据库 user = User() user.username = username user.headImg = headImg user.service_type = service_type user.updatetime = updatetime user.save() updatesql(headImg) print(headImg) return render(request, "register.html", { 'uf': uf, "put": "上传成功", "data": data, "data1": data1 }) else: uf = UserForm() # return render_to_response('register.html', {'uf': uf},) return render(request, "register.html", { 'uf': uf, "data": data, "data1": data1 })
def app01(request): User(username='******', password='******', age=20).save() return HttpResponse("<p>app01数据添加成功!</p>")
def sign_up_check(request): a = User() a.username = request.POST.get('username') a.password = request.POST.get('password') a.save() return render(request, 'my_index_app01/sign_up_jump.html')
import os if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') import django django.setup() from app01.models import User user_list = (User(name='用户{}'.format(i)) for i in range(100)) User.objects.bulk_create(user_list)