首先看一下数据模型类:
from django.db import models
class Enviconfig(models.Model):
"""环境模型类"""
envname = models.CharField(max_length=100, verbose_name='环境名称')
kafka_ip = models.CharField(max_length=100, verbose_name='Kafka Ip和端口')
zookeeper_ip = models.CharField(max_length=100,verbose_name='Zookeeper IP和端口')
mongo_ip = models.CharField(max_length=200, verbose_name='Mongo IP')
mongo_port = models.CharField(max_length=200, verbose_name='Mongo端口号')
redis_ip = models.CharField(max_length=211, verbose_name='Redis IP')
redis_passwd = models.CharField(max_length=211, verbose_name='Redis密码')
redis_db = models.CharField(max_length=211, verbose_name='Redis数据库名')
prmc_nginx = models.CharField(max_length=211, verbose_name='Prmc_nginx')
mysql_ip = models.CharField(max_length=211, verbose_name='Mysql IP')
mysql_user = models.CharField(max_length=211, verbose_name='Mysql用户')
mysql_passwd = models.CharField(max_length=211, verbose_name='Mysql密码')
oracle_ip = models.CharField(max_length=211, verbose_name='Oracle IP')
oracle_passwd = models.CharField(max_length=211, verbose_name='Oracle密码')
oracle_user = models.CharField(max_length=211, verbose_name='Oracle用户')
class Meta:
db_table = 'enviconfig'
verbose_name = '环境配置信息'
verbose_name_plural = verbose_name
def __str__(self):
return "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s"%(self.envname,self.kafka_ip,
self.zookeeper_ip, self.mongo_ip, self.mongo_port,
self.redis_ip, self.redis_passwd, self.redis_db,
self.prmc_nginx, self.mysql_ip, self.mysql_user,
self.mysql_passwd,self.oracle_ip,self.oracle_passwd,
self.oracle_user)
最后页面结果:
第一列是对字段的描述,第二列是数据值。第三列是input标签显示的数据值可以直接修改,点击确认修改即可提交数据库。
代码如下:
def get(self, request):
"""处理GET请求,返回页面"""
nick = request.session.get('nick')
qs = Enviconfig.objects.filter(envname='lan')
lan = qs[0].__str__()
env = lan.split(" ")
field_list = []
modelobj = apps.get_model('ywjenkins', 'Enviconfig')
for field in modelobj._meta.fields:
field_list.append(field.verbose_name)
field_list.pop(0)
dict1 = {}
for x in field_list:
dict1[x] = env[field_list.index(x)]
return render(request, 'jenkins/jenkinsconfig.html', {'nick': nick, 'dict':dict1})