def _edit_show(request, model, id): """渲染一个数据项的页面, 添加或者编辑""" # 获取tag名称 tag_name = BASE_ADMIN[model]['name'] # 获取model名称 model_name = BASE_ADMIN[model]['model'] # 获取得展示的字段 list_display = BASE_ADMIN[model]['list_display'] # 获取readyonly字段 if 'readonly' in BASE_ADMIN[model]: readonly_field = BASE_ADMIN[model]['readonly'] else: readonly_field = [] model_obj = None # 所有得添加add id为0 if id == 0: action = 'add' tag_name = u'添加' + tag_name data = get_data_from_model(model_name) else: action = 'modify' tag_name = u'编辑' + tag_name try: model_obj = model_name.objects.get(id=id) data = get_data_from_model(model_name, model_obj) except Exception, e: return render_to_response('404.html', locals(), context_instance=RequestContext(request))
def _edit_show(request, model, id): """渲染一个数据项的页面, 添加或者编辑""" # 获取tag名称 tag_name = BASE_ADMIN[model]['name'] # 获取model名称 model_name = BASE_ADMIN[model]['model'] # 获取得展示的字段 list_display = BASE_ADMIN[model]['list_display'] # 获取readyonly字段 if 'readonly' in BASE_ADMIN[model]: readonly_field = BASE_ADMIN[model]['readonly'] else: readonly_field = [] model_obj = None # 所有得添加add id为0 if id == 0: action = 'add' tag_name = '添加' + tag_name data = get_data_from_model(model_name) else: action = 'modify' tag_name = '编辑' + tag_name try: model_obj = model_name.objects.get(id=id) data = get_data_from_model(model_name, model_obj) except Exception as e: return render_to_response('404.html', locals(), context_instance=RequestContext(request)) # 设置foreignkey, 如果get_edit_context中有, 则取出并覆盖初始值: mchoices_keys = model_utils.get_foreignkeys_edit_context( model_name, model_obj) # 设置manytomany, 如果get_edit_context中有, 则取出并覆盖初始值: manytomany_keys = model_utils.get_manytomanys_edit_context( model_name, model_obj) return render_to_response('add_modify.html', locals(), context_instance=RequestContext(request))
def show_all(request, model): result = [] search_content = '' # 查看model是否在定义的模板中 if model in BASE_ADMIN: # 获取tag名称 tag_name = BASE_ADMIN[model]['name'] # 这个主要是编辑等一些动作 action_list = BASE_ADMIN[model]['action_list'] # 获取得展示的字段 list_display = BASE_ADMIN[model]['list_display'] # 获取model名称 model_name = BASE_ADMIN[model]['model'] # 是否有导入按钮 import_action = BASE_ADMIN[model]['import'] # 注意在model的字段注释要写,这样得话,表名的中文字段就可以直接从models中读取 table_field = get_data_from_model(model_name) # 得到数据 model_data = model_name.objects.all() # 得到条数 total_num = model_data.count() # 分页 page_list = [x + 1 for x in range(int(ceil(total_num / page_size)))] last_page = len(page_list) # 访问页码 page = request.GET.get('page', 1) page = int(page) # 取访问页的区间 page_zone = model_data[(page - 1) * page_size:page * page_size] # 处理数据 for one_data in page_zone: result.append(get_data_from_model(model_name, one_data)) else: # 如果没有跳转到404页面 return render_to_response('404.html', locals(), context_instance=RequestContext(request)) # 处理搜索 if request.method == 'POST': kargs = {} result = [] search_content = request.POST.get('search_content', '') search_key = request.POST.get('search_key', '') # 搜索功能 if table_field.get(search_key): field = table_field[search_key]['field'] field_type = table_field[search_key]['field_type'] if field_type in [SmallIntegerField, IntegerField]: tmp_search_text = int(search_content) kargs[field] = tmp_search_text if field_type in [ForeignKey]: # print model_name._meta.get_field(field).rel.to pass # kargs['%s_id' % field] = search_content else: kargs['%s__icontains' % field] = search_content search = Q(**kargs) search_data = model_name.objects.filter(search) total_num = search_data.count() for s_data in search_data: result.append(get_data_from_model(model_name, s_data)) return render_to_response('all_data_show.html', locals(), context_instance=RequestContext(request)) # 删除数据函数
def show_all(request, model): result = [] search_content = '' # 查看model是否在定义的模板中 if model in BASE_ADMIN: # 获取tag名称 tag_name = BASE_ADMIN[model]['name'] # 这个主要是编辑等一些动作 action_list = BASE_ADMIN[model]['action_list'] # 获取得展示的字段 list_display = BASE_ADMIN[model]['list_display'] # 获取model名称 model_name = BASE_ADMIN[model]['model'] # 是否有导入按钮 import_action = BASE_ADMIN[model]['import'] # 注意在model的字段注释要写,这样得话,表名的中文字段就可以直接从models中读取 table_field = get_data_from_model(model_name) #print("table_field is %s"%table_field) # 得到数据 model_data = model_name.objects.all() #print("model_data is %s"%model_data) # 得到条数 total_num = model_data.count() # 分页 page_list = [x + 1 for x in range(int(ceil(total_num / page_size)))] last_page = len(page_list) # 访问页码 page = request.GET.get('page', 1) page = int(page) # 取访问页的区间 page_zone = model_data[(page - 1) * page_size:page * page_size] # 处理数据 for one_data in page_zone: result.append(get_data_from_model(model_name, one_data)) else: # 如果没有跳转到404页面 return render_to_response('404.html', locals(), context_instance=RequestContext(request)) # 处理搜索 if request.method == 'POST': kargs = {} result = [] search_content = request.POST.get('search_content', '') search_key = request.POST.get('search_key', '') # 搜索功能 if table_field.get(search_key): field = table_field[search_key]['field'] field_type = table_field[search_key]['field_type'] if field_type in [SmallIntegerField, IntegerField]: tmp_search_text = int(search_content) kargs[field] = tmp_search_text if field_type in [ForeignKey]: # print model_name._meta.get_field(field).rel.to pass # kargs['%s_id' % field] = search_content else: kargs['%s__icontains' % field] = search_content search = Q(**kargs) search_data = model_name.objects.filter(search) total_num = search_data.count() for s_data in search_data: result.append(get_data_from_model(model_name, s_data)) #print("result is %s"%result) #print("locals is %s"%locals()) return render_to_response('all_data_show.html', locals(), context_instance=RequestContext(request)) # 删除数据函数