def password_reset(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admin[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) errors = {} if request.method == "POST": _password1 = request.POST.get("password1") _password2 = request.POST.get("password2") if _password1 == _password2: if len(_password1) > 5: obj.set_password(_password1) obj.save() return redirect(request.path.rstrip("password/")) else: errors["password_too_short"] = "密码长度不能少于6位!" else: errors["invalid_password"] = "******" return render(request, "admin/password_reset.html", { "obj": obj, "errors": errors })
def password_reset(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) errors = {} if request.method == "POST": _password1 = request.POST.get("password1") _password2 = request.POST.get("password2") if _password1 == _password2: if len(_password2) > 5: obj.set_password(_password1) obj.save() return redirect(request.path.rstrip("password/")) else: errors["password_too_short"] = "must not less than 6 letters" else: errors['invalid_password'] = "******" return render(request, 'king_admin/password_reset.html', { "obj": obj, "errors": errors })
def table_obj_change(request, app_name, table_name, obj_id): """ 编辑or修改功能 :param request: :param app_name: :param table_name: :param obj_id: :return: """ admin_class = king_admin.enabled_admin[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == "POST": form_obj = model_form_class(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() else: form_obj = model_form_class(instance=obj) return render( request, "admin/table_obj_change.html", { "form_obj": form_obj, "admin_class": admin_class, "app_name": app_name, "table_name": table_name })
def password_reset(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] admin_class.is_change_form = True model_form_class = create_model_form(request, admin_class, True) errors = {} obj = admin_class.model.objects.get(id=obj_id) if request.method == 'POST': _password1 = request.POST.get('password1') _password2 = request.POST.get('password2') if _password1 == _password2: if len(_password1) > 5: obj.set_password(_password1) obj.save() return redirect(request.path.rstrip('password/')) else: errors['password_too_short'] = 'must not less than 6 letters' else: errors['invalid_password'] = '******' return render(request, 'king_admin/password_reset.html', { 'obj': obj, 'errors': errors })
def table_obj_change(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == "POST": #request.POST.copy # for field in admin_class.readonly_fields: # field_val = getattr(obj,field) # request.POST[field] = field_val print(request.POST) form_obj = model_form_class(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() else: form_obj = model_form_class(instance=obj) #print("------->",getattr(obj,"name")) return render( request, "king_admin/table_obj_change.html", { "form_obj": form_obj, "admin_class": admin_class, "app_name": app_name, "table_name": table_name })
def record_change_index(request, app_name, table_name, record_id): """ :param request: 前端传的关键字等 :param app_name: app名 :param table_name: 表名 :param record_id: 要修改的记录对象 :return: """ admin_class = king_admin.enabled_admins[app_name][table_name] # 表类对象 model_form_class = forms.create_model_form(request, admin_class) # 动态添加form record_obj = admin_class.model.objects.get(id=record_id) # 获取的记录对象 if request.method == "POST": # 获取前端修改的form form_obj = model_form_class(request.POST, instance=record_obj) # 更新修改表单,获取表单对象 if form_obj.is_valid(): form_obj.save() # 获得报错返回前端显示 else: form_obj = model_form_class(instance=record_obj) # 让form中有对应的数据 # print("--------------------------------------11111111111",form_obj.errors.get('__all__')) # print("--------------------------------------",dir(form_obj['name'])) # print("--------------------------------------",form_obj['name'].value) # print("--------------------------------------", dir(form_obj['name'].errors)) # print("--------------------------------------",form_obj['name'].errors.data) return render(request, 'king_admin/record_change.html', { 'form_obj': form_obj, "app_name": app_name, "table_name": table_name })
def record_add_index(request, app_name, table_name): """ :param request: 前端传的关键字等 :param app_name: app名 :param table_name: 表名 :return: """ admin_class = king_admin.enabled_admins[app_name][table_name] admin_class.is_add_form = True # ?????????? model_form_class = forms.create_model_form(request, admin_class) print('8888888888888888888888888888888888', model_form_class) if request.method == "POST": form_obj = model_form_class(request.POST) # 给出空表单 ?????????????????? print(form_obj) if form_obj.is_valid(): print('00000000000000000000000000000000000000000000', form_obj.is_valid()) form_obj.save() # 把表单储存到数据库中 return redirect(request.path.replace("/add/", "/")) # ????????? else: form_obj = model_form_class() # 空表单 return render(request, 'king_admin/record_add.html', {'form_obj': form_obj})
def table_object_edit(request, app_name, table_name, obj_id): """ 编辑表中的一条数据 :param request: :param app_name: :param table_name: :param object_id: :return: """ admin_class = king_admin.enabled_admins[app_name][table_name] # 创建ModelForm类 model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == 'POST': form_obj = model_form_class(request.POST,instance=obj) # 更新 if form_obj.is_valid(): form_obj.save() else: form_obj = model_form_class(instance=obj) # return render(request,"king_admin/table_object_edit.html",{"form_obj":form_obj}) return render(request, 'king_admin/table_object_edit.html',{"form_obj": form_obj, "admin_class": admin_class, "app_name": app_name, "table_name": table_name})
def table_obj_add(request, app_name, table_name): """ 数据表添加记录 :param request: 请求的链接信息 :param app_name: :param table_name: :return: """ # 获取每个表的 model admin_class = king_admin.enabled_admins[app_name][table_name] f = request.path.split('/')[-2] admin_class.is_add_form = f # 动态创建 ModelForm 类对象 model_form_class = forms.create_model_form(request, admin_class) if request.method == 'POST': # 传入前端的数据,在传入数据库中的数据, 这时 form 会做更新操作, 如果只有前端数据, 只会新建一条记录 form_obj = model_form_class(request.POST) # 新增数据 if form_obj.is_valid: try: form_obj.save() return redirect(request.path.replace('/add/', '/')) # 增加完后跳转到信息表页面 except: pass else: # 实例化类对象 form_obj = model_form_class() return render(request, 'king_admin/table_obj_add.html', {'form_obj': form_obj, 'app_name': app_name, 'admin_class': admin_class})
def table_obj_add(request,app_name,table_name): admin_class = king_admin.enable_admins[app_name][table_name] admin_class.is_add_form = True model_form_class =create_model_form(request,admin_class) if request.method == "POST": form_obj = model_form_class(request.POST) if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace("/add/","/")) else: form_obj = model_form_class() return render(request,"king_admin/table_obj_add.html",{"form_obj":form_obj,"admin_class":admin_class})
def table_obj_add(request,app_name,table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) if request.POST: form_obj = model_form_class(request.POST) if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace('/add/','/')) else: form_obj = model_form_class() return render(request, "king_admin/table_obj_add.html", {"form_obj": form_obj, "admin_class": admin_class,})
def table_objs_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) if request.method == 'POST': #新增 form_obj = model_form_class(request.POST) if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace('/add', '/')) else: # obj = admin_class.model.objects.get(id=obj_id) form_obj = model_form_class() return render(request, 'king_admin/table_obj_add.html', {'form_obj': form_obj})
def table_obj_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) if request.method == "POST": form_obj = model_form_class(request.POST) # 新增 if form_obj.is_valid(): form_obj.save() redirect_path = request.path.replace('/add', '') return redirect(redirect_path) else: form_obj = model_form_class() return render(request, "king_admin/table_obj_add.html", {"form_obj": form_obj})
def table_obj_add(request,app_name,table_name): admin_class = king_admin.enabled_admins[app_name][table_name] admin_class.is_add_form=True model_for_class=create_model_form(request,admin_class) if request.method=='POST': form_obj=model_for_class(request.POST)#更新 POST是拿到已经修改的,ins是从数据库里面拿到的 if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace('/add','')) else: form_obj=model_for_class() return render(request, 'king_admin/table_obj_add.html', {'form_obj': form_obj, 'admin_class': admin_class })
def table_objs_add(request,app_name, table_name): admin_class = myadmin.enable_admin[app_name][table_name] model_form_class = forms.create_model_form(admin_class) admin_class.is_add_form = True if request.method == 'POST': model_form_obj = model_form_class(request.POST) if model_form_obj.is_valid(): model_form_obj.save() return redirect(request.path.replace('/add/','/')) else: model_form_obj = model_form_class() return render(request,'king_admin/table_objs_add.html',{'model_form_obj':model_form_obj, 'admin_class':admin_class})
def table_obj_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) if request.method == "POST": form_obj = model_form_class(request.POST, ) if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace('/add/', '/')) else: form_obj = model_form_class() return render(request, 'king_admin/table_obj_add.html', { 'form_obj': form_obj, 'admin_class': admin_class })
def table_obj_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) if request.method == "POST": form_obj = model_form_class(request.POST) #Add if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace("/add/", '/')) else: # get-method form_obj = model_form_class() # 表里有数据了 return render(request, "king_admin/table_obj_add.html", {"form_obj": form_obj})
def table_obj_change(request,app_name,table_name,obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request,admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.POST: form_obj = model_form_class(request.POST,instance = obj)#更新 if form_obj.is_valid(): form_obj.save() else: form_obj = model_form_class(instance = obj) return render(request,"king_admin/table_obj_change.html",{"form_obj":form_obj, "admin_class":admin_class, "app_name":app_name, "table_name":table_name})
def table_obj_change(request,app_name,table_name,obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class=create_model_form(request,admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == "POST": form_obj = model_form_class(request.POST, instance=obj) # 更新 if form_obj.is_valid(): form_obj.save() else: form_obj=model_form_class(instance=obj) return render(request,'king_admin/table_obj_change.html',{'form_obj':form_obj, 'admin_class':admin_class, 'app_name':app_name, 'table_name':table_name })
def table_obj_change(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == "POST": form_obj = model_form_class(request.POST, instance=obj) # 更新 if form_obj.is_valid(): form_obj.save() redirect_path = request.path.replace('/%s/change' % obj_id, '') return redirect(redirect_path) else: form_obj = model_form_class(instance=obj) return render(request, "king_admin/table_obj_change.html", {"form_obj": form_obj})
def table_obj_add(request, app_name, table_name): #表新增 admin_class = king_admin.enabled_admins[app_name][table_name] admin_class.is_add_form = True #表示是新增数据 model_obj = create_model_form(request, admin_class) if request.method == 'POST': form_obj = model_obj(request.POST) if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace('/add/', '/')) else: form_obj = model_obj return render(request, 'king_admin/table_add.html', { 'form_obj': form_obj, 'admin_class': admin_class })
def table_obj_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) admin_class.is_add_form = True #is_add_form这个属性用在自动生成form表单时,来判断是add页面还是change页面,add页面就不加disabled属性了 if request.method == "POST": form_obj = model_form_class(request.POST) # if form_obj.is_valid(): form_obj.save() return redirect(request.path.replace("/add/", "/")) # 即返回到表内容页面,而不是add页面!!! else: form_obj = model_form_class() return render(request, "king_admin/table_obj_add.html", {"form_obj": form_obj})
def table_objs_change(request,app_name, table_name, obj_id): admin_class = myadmin.enable_admin[app_name][table_name] admin_class.is_add_form = False model_form_class = forms.create_model_form(admin_class) obj = admin_class.model.objects.filter(id=obj_id).first() if request.method == 'POST': model_form_obj = model_form_class(request.POST, instance=obj) if model_form_obj.is_valid(): model_form_obj.save() else: model_form_obj = model_form_class(instance=obj) return render(request,'king_admin/table_objs_change.html',{'model_form_obj':model_form_obj, 'admin_class':admin_class, 'app_name':app_name, 'table_name':table_name, })
def table_obj_change(request, app_name, table_name, obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == "POST": form_obj = model_form_class( request.POST, instance=obj) #不加instance=是创建,会报错!加上之后,二者进行对比,有异处开始更新 if form_obj.is_valid(): form_obj.save() else: #get_method form_obj = model_form_class(instance=obj) #表里有数据了 return render(request, "king_admin/table_obj_change.html", { "form_obj": form_obj, "admin_class": admin_class })
def table_obj_add(request, app_name, table_name): '''添加用户''' admin_class = king_admin.enabled_admin[app_name][table_name] admin_class.add_form = True model_form_class = create_model_form(request, admin_class) if request.method == 'POST': form_obj = model_form_class(request.POST) # 更新 if form_obj.is_valid(): # 验证 form_obj.save() return redirect(request.path.replace('/add/', '/')) else: form_obj = model_form_class() return render(request, 'king_admin/table_obj_add.html', { 'form_obj': form_obj, 'app_name': app_name, 'table_name': table_name, })
def table_obj_change(req,app_name,table_name,obj_id): admin_class = kingadmin.enabled_admins[app_name][table_name] model_form_class = create_model_form(req,admin_class) obj = admin_class.model.objects.get(id=obj_id) if req.method == "POST": #此时提交过来的post请求是修改数据,为了让前端通过form显示修改后的数据,可以直接把post数据传给form #如果不给instance赋值,是创建,给了,才是修改 form_obj = model_form_class(req.POST,instance=obj) #更新 if form_obj.is_valid(): form_obj.save() else: form_obj = model_form_class(instance=obj) return render(req,'king_admin/table_obj_change.html',{'form_obj':form_obj, 'admin_class':admin_class, 'app_name':app_name, 'table_name':table_name})
def table_obj_add(req,app_name,table_name): admin_class = kingadmin.enabled_admins[app_name][table_name] admin_class.is_add_form = True model_form_class = create_model_form(req,admin_class) if req.method == "POST": #添加 print('add post data',req.POST) form_obj = model_form_class(req.POST) if form_obj.is_valid(): form_obj.save() return redirect(req.path.replace('/add/','/')) else: form_obj = model_form_class() print('end views') return render(req,'king_admin/table_obj_add.html',{'form_obj':form_obj, 'admin_class':admin_class})
def password_reset(request, app_name, table_name, obj_id): errors = {} admin_class = king_admin.enabled_admins[app_name][table_name] model_form_class = create_model_form(request, admin_class) obj = admin_class.model.objects.get(id=obj_id) if request.method == 'POST': password1 = request.POST.get('password1') password2 = request.POST.get('password2') if password1 == password2: obj.set_password(password1) obj.save() return redirect(request.path.rstrip("password/")) else: errors['error'] = "密码不一致" return render(request, 'king_admin/password_reset.html', { 'obj': obj, 'errors': errors })
def table_obj_change(request, app_name, model_name, obj_id): admin_class = king_admin.enabled_admins[app_name][model_name] obj = admin_class.model.objects.get(id=obj_id) model_form_class = create_model_form(request, admin_class) if request.method == 'GET': modelform_obj = model_form_class(instance=obj) elif request.method == 'POST': modelform_obj = model_form_class(request.POST, instance=obj) if modelform_obj.is_valid(): modelform_obj.save() else: print('errors', modelform_obj.errors) return render( request, 'king_admin/table_obj_change.html', { 'modelform_obj': modelform_obj, 'admin_class': admin_class, 'app_name': app_name, 'model_name': model_name })
def table_obj_add(request, app_name, table_name): admin_class = king_admin.enabled_admins[app_name][table_name] admin_class.is_add_form = True model_form_class = create_model_form(request, admin_class) if request.method == "POST": form_obj = model_form_class(request.POST) # if form_obj.is_valid(): form_obj.save() return JsonResponse({'success': "success"}) else: form_obj = model_form_class() return render( request, "myadmin/table_obj_add.html", { "form_obj": form_obj, "app_name": app_name, "table_name": table_name, 'admin_class': admin_class })