def table_obj_display(request, app_name, table_name): # # 通过反射根据表名获取表名的对象 # model_obj = getattr(models_module, table_name) # 不通过上面的反射获取model对象 直接通过app_name 和table_name 到king_admin字典里面获取 admin_class = site.enabled_admins[app_name][table_name] object_list, filter_conditions = pager.table_filter(request, admin_class) admin_class.filter_conditions = filter_conditions # 分页 # paginator = Paginator(object_list, admin_class.per_page) # print(paginator.page_range) # object_list = paginator.page(1) page_data = pager.split_page(object_list, page_num=request.GET.get('page'), per_page=admin_class.list_per_page) return_data = { 'enabled_admins': site.enabled_admins, 'app_name': app_name, 'table_name': table_name, "page_data": page_data, 'admin_class': admin_class, 'my_menu': permissions.get_permissions_menu(request.user), } return render(request, 'shuyuadmin/%s_display.html' % admin_class.display_html_pre, return_data)
def table_obj_update(request, app_name, table_name, nid): admin_class = site.enabled_admins[app_name][table_name] dynamic_form = create_model_form(admin_class, add=False) my_menu = permissions.get_permissions_menu(request.user) obj_id = nid icons = Icons.objects.all() try: obj = admin_class.model.objects.get(id=nid) except admin_class.model.DoesNotExist: return redirect('/shuyuadmin/%s/%s' % (app_name, table_name)) show_form = dynamic_form(instance=obj) errors_obj = {} errors = json.dumps(errors_obj) success = '' if request.method == 'GET': return render(request, 'shuyuadmin/table_obj_update.html', locals()) elif request.method == 'POST': obj = admin_class.model.objects.get(id=nid) show_form = dynamic_form(request.POST, instance=obj) if show_form.is_valid(): obj.save() success = '操作成功' else: errors = show_form.errors.as_json() return render(request, 'shuyuadmin/table_obj_update.html', locals())
def table_obj_show(request, app_name, table_name, nid): admin_class = site.enabled_admins[app_name][table_name] dynamic_form = create_model_form(admin_class, add=False, show=True) my_menu = permissions.get_permissions_menu(request.user) obj_id = nid try: obj = admin_class.model.objects.get(id=nid) except admin_class.model.DoesNotExist: return redirect('/shuyuadmin/%s/%s' % (app_name, table_name)) show_form = dynamic_form(instance=obj) return render(request, 'shuyuadmin/table_obj_show.html', locals())
def table_obj_add(request, app_name, table_name): admin_class = site.enabled_admins[app_name][table_name] dynamic_form = create_model_form(admin_class, add=True) my_menu = permissions.get_permissions_menu(request.user) icons = Icons.objects.all() errors_obj = {} errors = json.dumps(errors_obj) if request.method == 'GET': return render(request, 'shuyuadmin/table_obj_add.html', locals()) elif request.method == 'POST': show_form = dynamic_form(request.POST) if show_form.is_valid(): admin_class.model.objects.create(**show_form.cleaned_data) success = '操作成功' else: errors = show_form.errors.as_json() return render(request, 'shuyuadmin/table_obj_add.html', locals())
def table_obj_index(request): return_data = { 'enabled_admins': site.enabled_admins, 'my_menu': permissions.get_permissions_menu(request.user), } return render(request, 'shuyuadmin/table_obj_index.html', return_data)