def test(request): response1 = "" # Get all data list = User.objects.all() # Update Table # test1 = User.objects.get(id=2) # test1.name = 'aaa' # test1.save() User.objects.filter(id=2).update(name='Aaa') # Delete Data User.objects.filter(id=4).delete() # Insert data test1 = User(id='004', name='CCC', password='******', createdate=datetime.datetime.now(), updatedate=datetime.datetime.now(), status=1) test1.save() # Loop all data for var in list: response1 += var.name + " " return HttpResponse("<p>" + response1 + "</p>")
def useradddb(request): ctx = {} if request.POST: ctx['uname'] = request.POST['uname'] ctx['supassword'] = request.POST['supassword'] ctx['note'] = request.POST['note'] query_set = User.objects.filter(username=ctx['uname']) if query_set.exists(): messages.success(request, "ERROR!用户名已存在!") return render(request, "user-console-add.html") crtdb = User(username=ctx['uname'],password=ctx['supassword'],note=ctx['note']) crtdb.save() messages.success(request, "用户添加成功!") return render(request, "user-console-add.html")
def register(request): post_content = json.loads(request.body, encoding='utf-8') username = post_content['username'] password = post_content['password'] if User.objects.filter(username=username): return HttpResponse(json.dumps({'status': 1, 'msg': '已经被注册'})) else: try: user = User(id=User.objects.count(), username=username, password=password) user.save() return HttpResponse(json.dumps({'status': 0, 'msg': '注册成功'})) except: return HttpResponse(json.dumps({'status': 2, 'msg': '发生异常'}))
def insert(request): user = User(info={"name": "rivaill", "age": "3"}) user.save() user = User(info={"name": "rivaill2", "age": "4"}) user.save() user = User(info={"name": "rivaill3", "age": "5"}) user.save() user = User(info={"name": "rivaill4", "age": "6"}) user.save() user = User(info={"name": "rivaill5", "age": "7"}) user.save() return HttpResponse("<p>success</p>")
def register(request): if request.method == "POST": uf = UserForm(request.POST) if uf.is_valid(): #获取表单信息 username = uf.cleaned_data['username'] passworld = uf.cleaned_data['passworld'] email = uf.cleaned_data['email'] #将表单写入数据库 user = User() user.username = username user.passworld = passworld user.email = email user.save() #返回注册成功页面 return render_to_response('success.html', {'username': username}) else: uf = UserForm() return render_to_response('register.html', {'uf': uf})
def register(request): if request.session.get('is_login', None): return redirect('/') if request.method == "POST": log.debug('--> post') register_form = RegisterForm(request.POST) # log.info(register_form) message = '请检查填写的内容' if register_form.is_valid(): username = register_form.cleaned_data['username'] password1 = register_form.cleaned_data['password1'] password2 = register_form.cleaned_data['password2'] email = register_form.cleaned_data['email'] sex = register_form.cleaned_data['sex'] if password1 != password2: message = '两次输入的密码不同' return render(request, 'register.html', locals()) else: same_username = User.objects.filter(user_name=username) if same_username: message = "用户名已存在" return render(request, 'register.html', locals()) same_mail = User.objects.filter(email=email) if same_mail: message = "邮箱已存在" return render(request, 'register.html', locals()) new_user = User( user_name=username, password=md5_code(password1), # 使用md5加密 email=email, sex=sex) new_user.save() code = make_confirm_string(new_user) send_register_mail(email, code) message = "请前往注册邮箱,进行邮件确认" return render(request, 'confirm.html', locals()) # 跳到邮件确认页 register_form = RegisterForm() return render(request, 'register.html', locals())
def add_user(request): test1 = User(user_name='yy', user_pwd='123456a', user_mail='*****@*****.**') test1.save() return HttpResponse("用户创建成功,快去看看吧")