怎么用python+Element实现模板Temp操作
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容介绍了"怎么用python+Element实现模板Temp操作"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读
千家信息网最后更新 2025年12月03日怎么用python+Element实现模板Temp操作
本篇内容介绍了"怎么用python+Element实现模板Temp操作"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.前端HTML和CSS
修改 删除 立即创建 取消 立即创建 取消
2.前端JS
3.Django代码
urls.py文件内容
from django.conf.urls import patternsfrom home_application.temp import views as temp_viewurlpatterns = patterns( 'home_application.views', (r'^temp/$', 'temp'), (r'^temp_view/$', temp_view.TemplateView.as_view()), (r'^get_biz_list/$', 'get_biz_list'), (r'^export_temp/$', temp_view.export_temp), ...)
temp\views.py文件内容
import jsonimport xlwtfrom django.views.generic import Viewfrom django.views.decorators.csrf import csrf_exemptfrom django.utils.decorators import method_decoratorfrom django.http import JsonResponse, HttpResponsefrom django.db.models import Qfrom home_application.models import Templateclass CsrfExemptView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super(CsrfExemptView, self).dispatch(request, *args, **kwargs)class TemplateView(CsrfExemptView): def get(self, request, *args, **kwargs): search_biz_id = request.GET.get("search_biz_id") query_str = request.GET.get("query_str") try: temp_query = Template.objects.all() except Exception: return JsonResponse({"result": False}) if search_biz_id: temp_query = temp_query.filter(bk_biz_id=search_biz_id) if query_str: temp_query = temp_query.filter( Q(temp_name__icontains=query_str) | Q(script__contains=query_str) | Q(note__contains=query_str) ) res_data = [i.to_dict() for i in temp_query] return JsonResponse({"result": True, "data": res_data}) def post(self, request, *args, **kwargs): data = json.loads(request.body) temp_obj = { "bk_biz_id": data.get("add_bk_biz").split(":")[0], "bk_biz_name": data.get("add_bk_biz").split(":")[1], "temp_name": data.get("add_temp_name"), "note": data.get("add_temp_note"), "threshold": data.get("add_temp_value"), "script": data.get("add_temp_script"), } try: Template.objects.create(**temp_obj) return JsonResponse({"result": True}) except Exception: return JsonResponse({"result": False}) def put(self, request, *args, **kwargs): data = json.loads(request.body) pk = data.get("pk") bk_biz_id = data.get("edit_bk_biz").split(":")[0] bk_biz_name = data.get("edit_bk_biz").split(":")[1] temp_name = data.get("edit_temp_name") script = data.get("edit_temp_script") threshold = data.get("edit_temp_value") note = data.get("edit_temp_note") temp_obj = { "bk_biz_id": bk_biz_id, "bk_biz_name": bk_biz_name, "temp_name": temp_name, "script": script, "threshold": threshold, "note": note, } try: Template.objects.filter(pk=pk).update(**temp_obj) return JsonResponse({"result": True}) except Exception as e: print(e) return JsonResponse({"result": False}) def delete(self, request, *args, **kwargs): data = json.loads(request.body) pk = data.get("id") try: Template.objects.filter(pk=pk).delete() return JsonResponse({"result": True}) except Exception: return JsonResponse({"result": False})def generate_temp(): f = xlwt.Workbook() # 添加一个Sheet,名字为 sheet_name 所传的参数 sheet1 = f.add_sheet("Sheet1", cell_overwrite_ok=True) title = [u"模板ID", u"业务ID", u"业务名称", u"模板名称", u"巡检脚本", u"阀值", u"备注", u"创建时间"] # 写文件头 for i in range(0, len(title)): # i 表示第一行的第 i 列 sheet1.write(0, i, title[i]) temp_query = Template.objects.all() temp_list = [i.to_dict() for i in temp_query] # 从第二行开始写入数据 for i in range(0, len(temp_list)): # 向每一行的第1列写入数据 sheet1.write(i + 1, 0, temp_list[i].get("pk")) sheet1.write(i + 1, 1, temp_list[i].get("bk_biz_id")) # 向每一行的第2列写入数据 sheet1.write(i + 1, 2, temp_list[i].get("bk_biz_name")) sheet1.write(i + 1, 3, temp_list[i].get("temp_name")) sheet1.write(i + 1, 4, temp_list[i].get("script")) sheet1.write(i + 1, 5, temp_list[i].get("threshold")) sheet1.write(i + 1, 6, temp_list[i].get("note")) sheet1.write(i + 1, 7, temp_list[i].get("create_time")) f.save('模板.xls')def export_temp(request): generate_temp() file = open('模板.xls', 'rb') response = HttpResponse(file) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="模板.xls"' return responsemodels.py文件内容
from django.db import modelsfrom home_application.utils.parse_time import parse_datetime_to_timestrclass Template(models.Model): bk_biz_id = models.CharField(u"业务ID", max_length=8, blank=True, null=True) bk_biz_name = models.CharField(u"业务名称", max_length=32, blank=True, null=True) temp_name = models.CharField(u"模板名称", max_length=32, blank=True, null=True) script = models.TextField(u"巡检脚本", max_length=2048, blank=True, null=True) threshold = models.CharField(u"阀值", max_length=32, blank=True, null=True) note = models.CharField(u"备注", max_length=256, blank=True, null=True) create_time = models.DateTimeField(u"创建时间", auto_now_add=True) def to_dict(self): return { "pk": self.pk, "bk_biz_id": self.bk_biz_id, "bk_biz_name": self.bk_biz_name, "temp_name": self.temp_name, "script": self.script, "threshold": self.threshold, "note": self.note, "create_time": parse_datetime_to_timestr(self.create_time), }实现效果


"怎么用python+Element实现模板Temp操作"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
模板
业务
内容
名称
文件
成功
一行
数据
前端
备注
时间
更多
知识
脚本
更新
实用
学有所成
接下来
代码
参数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
深圳云服务器哪家好
全面加强村级网络安全建设通知
外网三大数据库
网络编程和网络安全哪个更好
win7支持sql数据库
linux如何加固服务器
电脑mc联机无法连接服务器
虹口区网络技术咨询零售价格
云服务器数据删了怎么办
开始数据库事务
数据库数据组织采用什么模型
软件开发与平面设计哪个工资高
贵州北斗校时服务器云空间
dns服务器验证
小学生网络安全在我身边班会
文章管理数据库字段设计
智能共享软件开发
海南冠尧网络技术有限公司怎么样
安徽存储服务器机箱采购云主机
查看华为云服务器配置
服务器必备指令2
人民日报社软件开发
消息推送服务器
博 期刊数据库
海南省网络安全数据
应用软件开发大脑
泰州个人软件开发推荐咨询
软件开发类监理
流量拦截软件开发
web服务器搭建常见问题