jango_modles_views显示
views
#!/usr/bin/env python #__coding:utf-8__ from django.shortcuts import render,render_to_response,HttpResponse,redirect import models # Create your views here. def register(request): usertype_table_td1 = models.usertype.objects.create(name='管理员') usertype_table_td2 = models.usertype.objects.create(name='普通用户') usertype_table_obj1 = models.usertype.objects.get(name='管理员') usertype_table_obj2 = models.usertype.objects.get(name='普通用户') u1 = models.userinfo.objects.create(username='fengjian',password='123456',email='fengjian@163.com',usertype_id=usertype_table_td1) u2 = models.userinfo.objects.create(username='su',password='123456',email='su@163.com',usertype_id=usertype_table_td2) groupobj1 = models.usergroup.objects.create(groupname='dba组') groupobj2 = models.usergroup.objects.create(groupname='ATG组') groupobj1.user_group_manytomany.add(u1) groupobj2.user_group_manytomany.add(u2) return HttpResponse('注册成功!') def login(request): ret = {'status':""} if request.method == 'POST': username = request.POST.get('username',None) password = request.POST.get('password',None) result = all([username,password]) if result: data = models.userinfo.objects.filter(username=username,password=password) if data: return redirect('/web/index/') else: ret['status'] = '用户名密码输入错误' else: ret['status'] = '用户名密码不能为空' return render_to_response('login.html',ret) def index(request): return render_to_response('index.html') def addhost(request): ret={'status':"",'group':None,'data':None} usergroupdata = models.usergroup.objects.all() ret['group'] = usergroupdata if request.method == 'POST': hostname = request.POST.get('hostname',None) ip = request.POST.get('ip',None) usergroup_id = request.POST.get('group',None) is_empty = all([hostname,ip]) if is_empty: usergroupobj = models.usergroup.objects.get(id = usergroup_id) models.asset.objects.create(hostname = hostname,ip = ip,usergroup_id = usergroupobj) else: ret['status'] = '主机名或者IP不能为空' assetdata = models.asset.objects.all() ret['data'] = assetdata obj = models.asset.objects.filter(usergroup_id__groupname='DBA组') asslist = obj1 = models.asset.objects.filter(usergroup_id__id=1) for item in asslist: print item return render_to_response('host.html',ret)
models.py
from __future__ import unicode_literals from django.db import models # Create your models here. class usertype(models.Model): name = models.CharField(max_length=40) class userinfo(models.Model): username = models.CharField(max_length=40) password = models.CharField(max_length=40) email = models.CharField(max_length=40) usertype_id = models.ForeignKey('usertype') class usergroup(models.Model): groupname = models.CharField(max_length=40) user_group_manytomany = models.ManyToManyField('userinfo') class asset(models.Model): hostname = models.CharField(max_length=40) ip = models.GenericIPAddressField() usergroup_id = models.ForeignKey('usergroup')