CRM手記-9 | Django
阿新 • • 發佈:2018-01-21
html中 () 錯誤 handle blog ttr 增刪改查 quest 自定義樣式
# 增刪改查 # 前端提交數據的方式: # ajax|form表單 # 前端數據校驗,為了減小服務器不必要的壓力; # 後端驗證是為了保證後端數據的準確性; # 因為表格中數據都是在後端simple_tag生成的,所以需要在simple_tag循環代碼中, # 將下標為0的字段值上加上鏈接,點擊可以跳轉至內容修改頁面; # kingadmin_tags.py @register.simple_tag def build_table_row(obj,admin_class): ele = "" ifadmin_class.list_display: for index,column_name in enumerate(admin_class.list_display): column_obj = admin_class.model._meta.get_field(column_name) if column_obj.choices: #get_xxx_display column_data = getattr(obj,‘get_%s_display‘% column_name)() else: column_data = getattr(obj,column_name) td_ele = "<td>%s</td>"% column_data # 如果index為0時,為該字段值添加上鏈接 if index == 0: td_ele = "<td><a href=‘%s/change/‘></a>%s</td>"% (obj.id,column_data) ele += td_ele else: td_ele = "<td><a href=‘%s/change/‘>%s</a></td>"% (obj.id,obj) ele += td_ele return mark_safe(ele) # 配置鏈接的urlconf # urls.py url(r‘^/(\w+)/(\w+)/(\d+)/change/$‘, views.table_obj_change, name="table_obj_change"), # views.py @login_required def table_obj_change(request,app_name,model_name,obj_id): """數據修改頁面""" return render(request, ‘kingadmin/table_obj_change.html‘) # table_obj_change.html {% extends ‘kingadmin/index.html‘ %} {% load kingadmin_tag %} {% block right_content %} The content for urself! <div> 修改頁面! </div> {% endblock right_content %} # 靜態model_form回顧 # forms.py class CustomerForm(ModelForm): class Meta: model = models.CustomerInfo fields = ‘__all__‘ #[‘name‘,‘consultant‘,‘status‘] # views.py @login_required def table_obj_change(request,app_name,model_name,obj_id): """數據修改頁面""" from crm import forms form = forms.CustomerForm() return render(request, ‘kingadmin/table_obj_change.html‘,locals()) # form數據就在頁面中顯示,但是所有表頁面顯示這一個modelform指定的表字段; # 難道每個表都寫一個modelform? # 怎麽做成動態modelform?django裏沒有動態modelform的概念; # 動態生成類? # 創建類的另一種方式 def func(self): # 記得加self print(‘hello world‘) Foo = type(‘Foo‘,(object,),{‘func‘:func}) # 參數1:類名; # 參數2:當前類的基類; # 參數3:類的成員; # 實現動態生成指定表的所有表結構modelform # form_handle.py # class CustomerForm(ModelForm): # """展示所有表結構""" # class Meta: # model = models.CustomerInfo # fields = ‘__all__‘ #[‘name‘,‘consultant‘,‘status‘] def create_dynamic_modelform(admin_class): """動態生成modelform""" class Meta: model = admin_class.model fields = "__all__" dynamic_form = type("DynamicModelForm",(ModelForm,),{‘Meta‘:Meta}) print(dynamic_form) # >> <class ‘django.forms.widgets.DynamicModelForm‘> return dynamic_form # 返回類,來實例化 # veiws.py @login_required def table_obj_change(request,app_name,model_name,obj_id): """數據修改頁面""" # 1.動態創建modelform類 admin_class = site.enabled_admins[app_name][model_name] modelform = form_handle.create_dynamic_modelform(admin_class) # 2.實現modelform,展示表結構 form_obj = modelform() return render(request, ‘kingadmin/table_obj_change.html‘,locals()) # modelform添加自定義樣式 # <div class="form-group"> # <label for="inputEmail3" class="col-sm-2 control-label">Email</label> # <div class="col-sm-10"> # <input type="email" class="form-control" id="inputEmail3" placeholder="Email"> # </div> # </div> # table_obj_change.html <form class="form-horizontal"> {% for field in form_obj %} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">{{ field.label }}</label> <div class="col-sm-10"> {{ field }} </div> </div> {% endfor %} </form> # {{field.label}} 自動生成了input樣式,怎麽修改? # 修改modelform樣式 # form_handle.py def create_dynamic_modelform(admin_class): """動態生成modelform""" class Meta: model = admin_class.model fields = "__all__" def __new__(cls,*args,**kwargs): print("__new__",cls,args,kwargs) for field_name in cls.base_fields: field_obj = cls.base_fields[field_name] field_obj.widget.attrs.update({‘class‘:‘form-control‘}) return ModelForm.__new__(cls) dynamic_form = type("DynamicModelForm",(ModelForm,),{‘Meta‘:Meta,‘__new__‘:__new__}) print(dynamic_form) return dynamic_form # 任意表的增刪改查 # 1.修改 # 1.1 展示當前修改表與記錄 # table_obj_change.html <nav class="navbar navbar-default navbar-static-top"> <div class="container"> <!-- 1.獲取當前表名 --> <h1>{% get_model_name admin_class %}</h1> <!-- 2.顯示當前修改哪條記錄 --> 當前記錄:{{ form_obj.instance }} </div> </nav> # views.py @login_required def table_obj_change(request,app_name,model_name,obj_id): """數據修改頁面""" # 1.動態創建modelform類 admin_class = site.enabled_admins[app_name][model_name] modelform = form_handle.create_dynamic_modelform(admin_class) # 2.實現modelform,展示表結構 # 2.1 展示沒有數據的modelform # form_obj = modelform() # 2.2 展示當前數據的modelform # 獲取當前記錄對象 obj = admin_class.model.objects.get(id=obj_id) form_obj = modelform(instance=obj) return render(request, ‘kingadmin/table_obj_change.html‘,locals()) # 提交修改後的表單model_form <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Sign in</button> </div> </div> # 1.2 提交以及保存修改後的數據 # views.py @login_required def table_obj_change(request,app_name,model_name,obj_id): """數據修改頁面""" # 1.動態創建modelform類 admin_class = site.enabled_admins[app_name][model_name] modelform = form_handle.create_dynamic_modelform(admin_class) # 2.實現modelform,展示表結構 # 2.1 展示沒有數據的modelform # form_obj = modelform() # 2.2 展示當前數據的modelform # 獲取當前記錄對象 obj = admin_class.model.objects.get(id=obj_id) # 3.判斷請求條件,如果是POST請求,獲取請求數據,進行數據檢驗,再做保存,驗證不通過,返回錯誤信息; if request.method == ‘GET‘: form_obj = modelform(instance=obj) elif request.method == ‘POST‘: form_obj = modelform(instance=obj,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_change.html‘,locals()) # table_obj_change.html <form class="form-horizontal" method="POST"> {% csrf_token %} <!-- modelform全局錯誤信息 --> {# {{ form_obj.errors }}#} {% for field in form_obj %} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">{{ field.label }}</label> <div class="col-sm-10"> {{ field }} <!-- 單字段錯誤信息 --> <span style="color:red">{{ field.errors.0 }}</span> </div> </div> {% endfor %} <div class="form-group"> <div class="col-sm-offset-11 col-sm-10"> <button type="submit" class="btn btn-default">SAVE</button> </div> </div> </form> # 2.增加 # urls.py url(r‘^(\w+)/(\w+)/add/$‘, views.table_obj_add, name="table_obj_add"), # views.py @login_required def table_obj_add(request,app_name,model_name): """增加記錄""" # 1.動態創建modelform類 admin_class = site.enabled_admins[app_name][model_name] modelform = form_handle.create_dynamic_modelform(admin_class) if request.method == ‘GET‘: form_obj = modelform() elif request.method == ‘POST‘: form_obj = modelform(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()) # table_obj_add.html # table_obj_change.html中的表結構就不重復寫了,單獨寫在table_obj_common.html,其它頁面include它; # {% include ‘kingadmin/table_obj_common.html‘ %} {% extends ‘kingadmin/index.html‘ %} {% load kingadmin_tag %} {% block right_content %} <nav class="navbar navbar-default navbar-static-top"> <div class="container"> <!-- 1.獲取當前表名 --> <h1>{% get_model_name admin_class %}</h1> <!-- 2.顯示當前修改哪條記錄 --> <b>添加</b> </div> </nav> {% include ‘kingadmin/table_obj_common.html‘ %} {% endblock right_content %} # 問題:不知道有沒添加? # 措施:將最新添加的顯示在第一個; # views.py def table_obj_list(request,app_name,model_name): ... querysets = admin_class.model.objects.all().order_by(‘-id‘) ... ############################################################### 修改與添加功能做完了;
CRM手記-9 | Django