def change_view(self, request, pk, *args, **kwargs): """ 编辑页面 :param request: :return: """ # 当前编辑对象 current_change_object = self.model_class.objects.filter(pk=pk).first() if not current_change_object: return HttpResponse("需要修改的对象不存在,请重新选择") model_form_class = self.get_model_form_class(is_add=False) if request.method == "GET": form = model_form_class(instance=current_change_object) return render(request, 'stark/change.html', {"form": form}) form = model_form_class(data=request.POST, instance=current_change_object) if form.is_valid(): self.form_database_save(request, form, True, *args, **kwargs) # 在数据库中保存成功后,跳转回列表页面(携带原来的参数) parse_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_list_url_name) url = parse_url.memory_url() return redirect(url) return render(request, 'stark/change.html', {"form": form})
def get_add_btn(self): if self.has_add_btn: # 根据别名反向生成URL parse_url = ParseUrl(self.request,self.site.namespace,self.get_add_url_name) url = parse_url.memory_reverse_url() return '<a class="btn btn-primary" href="%s">添加</a>'%(url) return None
def display_reset_pwd(self, obj=None, is_header=None): if is_header: return "重置密码" parse_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_url_name('reset_pwd'), pk=obj.pk) url = parse_url.memory_url() return mark_safe('<a href="%s">重置密码</a>' % url)
def display_del(self, obj=None, is_header=None): if is_header: return "删除" parse_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_delete_url_name, pk=obj.pk) url = parse_url.memory_reverse_url() return mark_safe('<a href="%s">删除</a>' % url)
def display_record(self,obj=None,is_header=None): if is_header: return '跟进记录' record_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_url_name('record_view'), pk=obj.pk) url = record_url.memory_url() return mark_safe('<a href="%s">查看跟进</a>'%url)
def display_edit(self, obj=None, is_header=None): """ 自定义页面显示的列(表头和内容) :param obj: :param is_header: :return: """ if is_header: return "编辑" parse_url = ParseUrl(request=self.request,namespace=self.site.namespace,name=self.get_change_url_name,pk=obj.pk) url = parse_url.memory_reverse_url() return mark_safe('<a href="%s">编辑</a>' % url )
def delete_view(self, request, pk): """ 删除页面 :param request: :param pk: :return: """ parse_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_list_url_name) cancel = parse_url.memory_url() if request.method == "GET": return render(request,'stark/delete.html',{'cancel':cancel}) current_delete_object = self.model_class.objects.filter(pk=pk).delete() return redirect(cancel)
def add_view(self, request): """ 添加页面 :param request: :return: """ model_form_class = self.get_model_form_class() if request.method == "GET": form = model_form_class() return render(request,'stark/change.html',{"form":form}) form = model_form_class(data=request.POST) if form.is_valid(): self.form_database_save(form,is_update=False) # 在数据库中保存成功后,跳转回列表页面(携带原来的参数) parse_url = ParseUrl(request=self.request,namespace=self.site.namespace,name=self.get_list_url_name) url = parse_url.memory_url() return redirect(url) return render(request, 'stark/change.html', {"form": form})
def reset_password(self, request, pk): """ 重置密码的视图函数 :param request: :param pk: :return: """ userinfo_object = models.UserInfo.objects.filter(id=pk).first() if not userinfo_object: return HttpResponse("用户不存在,无法进行重置!") if request.method == "GET": form = ResetPasswordForm() return render(request, 'stark/change.html', {'form': form}) form = ResetPasswordForm(data=request.POST) if form.is_valid(): userinfo_object.password = form.cleaned_data['password'] userinfo_object.save() parse_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_list_url_name) url = parse_url.memory_url() return redirect(url) return render(request, 'stark/change.html', {'form': form})
def display_edit_del(self, obj=None, is_header=None): if is_header: return "操作" parse_edit_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_change_url_name, pk=obj.pk) edit_url = parse_edit_url.memory_reverse_url() parse_delete_url = ParseUrl(request=self.request, namespace=self.site.namespace, name=self.get_delete_url_name, pk=obj.pk) delete_url = parse_delete_url.memory_reverse_url() tpl = '<a href="%s">编辑</a>||<a href="%s">删除</a>' % (edit_url, delete_url) return mark_safe(tpl)