Django之前端插件定制之表头
什么是插件?
插件只是辅助,是开发过程中的一个阶段。一般项目一期会用各种插件,迅速将功能、界面搭出来,二期时就改成自己的代码了。大点的公司都有自己的js库,自己开发类似jquery的库。
那接下来就写一下关于前端表头的插件
urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
#以前用FBV写,现在用CBV写
path('server.html', views.ServerView.as_view()),
path('server-json.html', views.ServerJsonView.as_view()),
]
urlpatterns += staticfiles_urlpatterns()
models.py
from django.db import models class UserInfo(models.Model):
name=models.CharField(max_length=64)
age=models.IntegerField() class BusinessUnit(models.Model):
name=models.CharField(max_length=64) class Server(models.Model):
server_type_choices=(
(1,'Web'),
(2,'存储'),
(3,'缓存')
)
server_type=models.IntegerField(choices=server_type_choices)
hostname=models.CharField(max_length=64)
port=models.IntegerField()
business_unit=models.ForeignKey(to="BusinessUnit",to_field="id",on_delete=models.CASCADE)
user=models.ForeignKey(to='UserInfo',to_field="id",on_delete=models.CASCADE)
views.py
from django.shortcuts import render,HttpResponse
from django.views import View
import json class BaseResponse(object): # 封装数据 def __init__(self):
self.status=True
self.data= None
self.message=None class ServerView(View): # 这个插件的增删改查全部是利用Ajax去做的 def get(self,request,*args,**kwargs): return render(request,'server.html') class ServerJsonView(View): # 前端利用Ajax把前端的数据发送到这里进行处理和发送 def get(self,request,*args,**kwargs):
response=BaseResponse()
try:
# 获取现实的列
# 获取数据
table_config=[
{
'title':'主机名',
'display':1,
},
{
'title':'端口',
'display':1,
},
]
response.data={ # 配置文件
'table_config':table_config, # 那么这个列表就传输到前端页面了
}
except Exception as e:
response.status=False # 如果出错
response.message=str(e)
return HttpResponse(json.dumps(response.__dict__)) #把数据发送到前端的用户
server.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/js/jquery-1.12.4.js"></script>
</head>
<body>
<table border="">
<thead id="thead"></thead>
<tbody id="tbody"></tbody>
</table> <script>
$(function () {
init();
}); function init() {
//获取现实的列
//获取数据
$.ajax({
url:'server-json.html',
type:'GET',
dataType:'JSON',
success:function (arg) {
if(arg.status){
//创建表格标题
createTablehead(arg.data.table_config); // 后台的response.data里的table_config
}else{
alert(arg.message)
} }
})
} var tr=document.createElement('tr'); // 创建个tr标签
function createTablehead(config) { //config=response.data.table_config
$.each(config,function (k,v) { // $.each 为循环 对config循环 v代指字典
if(v.display){ // 若v.display == True 那么创建一个th
var th=document.createElement('th'); // 创建th标签
th.innerHTML=v.title; // 把后台的table_config里的title放入到th标签内
$(tr).append(th); // 把th标签添加到tr标签里
}
}) $('#thead').append(tr); // 把tr标签添加到thead标签里
} </script>
</body>
</html>
那么,现在看一下前端界面的显示结果吧!

那么,关于前端插件定制之表头就已经完成了!
Django之前端插件定制之表头的更多相关文章
- Django使用DataTables插件总结
Django使用Datatables插件总结 文章中的例子已上传至github 基本使用 Datatables插件是一款方便简单的展示数据的列表插件.关于基本使用,官方网站上的已介绍的很详细,这里我再 ...
- django form 组件插件
创建类: class RegForms(forms.Form): account = fields.CharField( required = True, #必填字段 max_length=12, m ...
- 前端插件之Datatables使用--下篇
工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,上一篇文章介绍了Datatables插件的基本使用,这一篇文章作为上一篇的延续,会介绍Databases的一些高级 ...
- 前端插件@user
分享一个 @user 前端插件 开源地址:https://github.com/yuezhongxin/Mention.js 插件效果:类似于微博或 github 中 @user 列表效果. 这是 ...
- django admin后台插件:django-suit入门
去年9月底开始用django来做公司内部项目,开始对django有了一些了解,感觉django真的蛮强大的(也有很多人推荐flask,将来有空的话我会试试).今天的话只是介绍一个小东西,django管 ...
- 前端插件之Bootstrap Dual Listbox使用
工欲善其事,必先利其器 对于很多非专业前端开发来说写页面是非常痛苦的,借助框架或插件往往能够达到事半功倍的效果,本系列文章会介绍我在运维系统开发过程中用到的那些顺手的前端插件,如果你是想写XX管理系统 ...
- 前端插件之Select2使用
工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,上一篇文章介绍了bootstrap-duallistbox这个插件的使用,这一篇开始Select2的征服之旅 Se ...
- 前端插件之Datatables使用--上篇
工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,前边两篇分别介绍了Duallistbox插件和Select2插件的使用,这一篇开始Databases的征服之旅 D ...
- VSCODE开发VUE.JS前端插件
VUE前端插件.转载自:https://www.cnblogs.com/karthuslorin/p/8577224.html vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscod ...
随机推荐
- webpack打包多html开发案例
简单说明: 1,案例没有使用devserver,所以不能实时查看 2,案例是将src下面的html.css.js分别进行处理 3,多html的规律是需要有多个entry,每个html一个entry,当 ...
- win7 64位机ODBC的数据源DSN添加和移除问题
64位机器上ODBC的操作方法与32位机器是不一样的,如果直接从控制面板上-管理员工具-ODBC进去的话会发现User DSN以及System DSN里面都为空,ADD的时候连ODBC Driver都 ...
- vue+postcss报错: variable '--primary-color' is undefined and used without a fallback
之前vue-cli3引入postcss的配置: https://www.cnblogs.com/XHappyness/p/7676680.html 发现这么一个问题,我再全局global.css中定义 ...
- HDU——1788 Chinese remainder theorem again
再来一发水体,是为了照应上一发水题. 再次也特别说明一下,白书上的中国剩余定理的模板不靠谱. 老子刚刚用柏树上的模板交上去,简直wa出翔啊. 下面隆重推荐安叔版同余方程组的求解方法. 反正这个版本十分 ...
- 【bzoj1004】[HNOI2008]Cards Burnside引理+背包dp
题目描述 用三种颜色染一个长度为 $n=Sr+Sb+Sg$ 序列,要求三种颜色分别有 $Sr,Sb,Sg$ 个.给出 $m$ 个置换,保证这 $m$ 个置换和置换 ${1,2,3,...,n\choo ...
- byte数组转float实现与byte转换其它类型时进行&运算原理
下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] &a ...
- 【BZOJ1001】狼抓兔子(平面图转对偶图,最短路)
[BZOJ1001]狼抓兔子(平面图转对偶图,最短路) 题面 BZOJ 洛谷 题解 这题用最小割可以直接做 今天再学习了一下平面图转对偶图的做法 大致的思路如下: 1.将源点到汇点中再补一条不与任何线 ...
- BZOJ3527:[ZJOI2014]力——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3527 给出n个数qi,给出Fj的定义如下: 令Ei=Fi/qi,求Ei. 参考:https://ww ...
- 使当前对象相对于上层DIV 水平、垂直居中定位
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- Git入门指南
git学习资源: Pro Git(中文版) Learn Git in your browser for free with Try Git. Git常用命令 Reference 常用 Git 命令清单 ...