form验证 属性required=False 浏览器不验证
{{ fIEld.error.0 }} 不加0会显示列表形式
form_obj.save() 保存数据更新
from django.shortcuts import render,redirectfrom django.contrib.auth import authenticate,login,logoutfrom django.contrib.auth.decorators import login_requiredfrom django import conffrom django.db.models import Qfrom django.core.paginator import Paginator,PageNotAnInteger,EmptyPagefrom kingadmin import app_setupfrom kingadmin import form_handleapp_setup.kingadmin_auto_discover()from kingadmin.sites import siteprint("sites.",site.enabled_admins)# for k,v in site.enabled_admins.items():# for table_name,admin_class in v.items():# print(table_name,ID(admin_class))# # Create your vIEws here.def app_index(request): #enabled_admins = return render(request,‘kingadmin/app_index.HTML‘,{‘site‘:site})def get_filter_result(request,querysets): filter_conditions = {} for key,val in request.GET.items(): if key in (‘_page‘,‘_o‘,‘_q‘):continue if val: filter_conditions[key] = val print("filter_conditions",filter_conditions) return querysets.filter(**filter_conditions),filter_conditionsdef get_orderby_result(request,querysets,admin_class): """排序""" current_ordered_column = {} orderby_index = request.GET.get(‘_o‘) if orderby_index: orderby_key = admin_class.List_display[ abs(int(orderby_index)) ] current_ordered_column[orderby_key] = orderby_index #为了让前端知道当前排序的列 if orderby_index.startswith(‘-‘): orderby_key = ‘-‘+ orderby_key return querysets.order_by(orderby_key),current_ordered_column else: return querysets,current_ordered_columndef get_serached_result(request,admin_class): search_key = request.GET.get(‘_q‘) if search_key : q = Q() q.connector = ‘OR‘ for search_fIEld in admin_class.search_fIElds: q.children.append(("%s__contains"% search_fIEld,search_key)) return querysets.filter(q) return querysets# @login_requireddef table_obj_List(request,app_name,model_name): """取出指定model里的数据返回给前端""" #print("app_name,model_name:",site.enabled_admins[app_name][model_name]) admin_class = site.enabled_admins[app_name][model_name] querysets = admin_class.model.objects.all().order_by(‘-ID‘) querysets,filter_condtions = get_filter_result(request,querysets) admin_class.filter_condtions = filter_condtions #searched queryset result querysets = get_serached_result(request,admin_class) admin_class.search_key = request.GET.get(‘_q‘,‘‘) #sorted querysets querysets,sorted_column = get_orderby_result(request,admin_class) paginator = Paginator(querysets,2) # Show 25 contacts per page page = request.GET.get(‘_page‘) try: querysets = paginator.page(page) except PageNotAnInteger: # If page is not an integer,deliver first page. querysets = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999),deliver last page of results. querysets = paginator.page(paginator.num_pages) print(request.GET) #print("admin class",admin_class.model ) return render(request,‘kingadmin/table_obj_List.HTML‘,{‘querysets‘:querysets,‘admin_class‘:admin_class,‘sorted_column‘:sorted_column})# @login_requireddef table_obj_change(request,model_name,obj_ID): """kingadmin 数据修改页""" admin_class = site.enabled_admins[app_name][model_name] model_form = form_handle.create_dynamic_model_form(admin_class) obj = admin_class.model.objects.get(ID=obj_ID) if request.method == "GET": form_obj = model_form(instance=obj) elif request.method == "POST": form_obj = model_form(instance=obj,data=request.POST) if form_obj.is_valID(): form_obj.save() return redirect("/kingadmin/%s/%s/" %(app_name,model_name)) # from crm.forms import CustomerForm # # form_obj = CustomerForm() return render(request,‘kingadmin/table_obj_change.HTML‘,locals())def table_obj_add(request,model_name): admin_class = site.enabled_admins[app_name][model_name] model_form = form_handle.create_dynamic_model_form(admin_class,form_add=True) if request.method == "GET": form_obj = model_form() elif request.method == "POST": form_obj = model_form(data=request.POST) if form_obj.is_valID(): form_obj.save() return redirect("/kingadmin/%s/%s/" % (app_name,model_name)) return render(request,‘kingadmin/table_obj_add.HTML‘,locals())def acc_login(request): error_msg = ‘‘ if request.method == "POST": username = request.POST.get(‘username‘) password = request.POST.get(‘password‘) user = authenticate(username=username,password=password) if user: print("passed authencation",user) login(request,user) #request.user = user return redirect( request.GET.get(‘next‘,‘/kingadmin/‘) ) else: error_msg = "Wrong username or password!" return render(request,‘kingadmin/login.HTML‘,{‘error_msg‘:error_msg})def acc_logout(request): logout(request) return redirect("/login/")vIEws 总结
以上是内存溢出为你收集整理的CRM项目实战-实现任意表的增删改查全部内容,希望文章能够帮你解决CRM项目实战-实现任意表的增删改查所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)