models.py
#coding:utf8
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.db import models
from django.contrib import admin
# Create your models here.
class Host(models.Model):
hostname=models.CharField(max_length=32,verbose_name='主机名')
ip_addr=models.GenericIPAddressField(verbose_name='IP地址')
port=models.IntegerField(verbose_name='端口号')
system=models.CharField(max_length=64,verbose_name='操作系统',choices=
(
('windows','Windows'),
('linux','Linux'),
))
idc=models.ForeignKey('Idc',verbose_name='IDC')
group=models.ManyToManyField('Group',verbose_name='用户组')
def __unicode__(self):
return self.hostname
class Idc(models.Model):
name=models.CharField(max_length=32,verbose_name='idc名称')
def __unicode__(self):
return self.name
class Group(models.Model):
name=models.CharField(max_length=32,verbose_name='组名')
def __unicode__(self):
return self.name
class Userpfile(models.Model):
user=models.OneToOneField(User,verbose_name='用户')
nick_name=models.CharField(max_length=12,verbose_name='昵称')
group=models.ManyToManyField('Group',verbose_name='管理组')
def __unicode__(self):
return self.nick_name
urls.py
from django.conf.urls import url
from django.contrib import admin
from son1.views import *
from son1 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',index),
url(r'^login/$',acc_login),
url(r'^logout/$',acc_logout),
url(r'^host_save/$',host_save),
url(r'^del/(d+)$',delhost),
]
views.py
from django.shortcuts import render,HttpResponseRedirect,HttpResponse
from son1 import models
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
id=request.GET.get('hostid')
if request.GET.get('hostid')is not None:
host_obj=models.Host.objects.get(id=id)
print host_obj
return render(request,'index.html',locals())
def host_save(request):
host_obj=models.Host.objects.get(id=request.GET.get('host_id'))
host_obj.hostname=request.GET.get('hostname')
host_obj.ip_addr=request.GET.get('ip_addr')
host_obj.port=request.GET.get('port')
host_obj.save()
return HttpResponse('ok')
def delhost(request,id):
try:
host_obj=models.Host.objects.get(id=id)
host_obj.delete()
except Exception as e:
pass
return HttpResponseRedirect('/')
def acc_login(request):
username=request.GET.get('username')
password=request.GET.get('password')
user=authenticate(username=username,password=password)
if user is not None:
login(request,user)
return HttpResponseRedirect('/')
return render(request,'login.html',locals())
def acc_logout(request):
logout(request)
return HttpResponseRedirect('/')
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.hostinfor{
color: pink;
}
</style>
<title>运维平台</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
<link rel="stylesheet" href="{% static 'css/navbar-static-top.css' %}">
</head>
<body>
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="nav-header">
<a class="navbar-brand" href="#">1502b信息</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">首页</a></li>
<li><a href="/">主机管理</a></li>
<li><a href="/">资产管理</a></li>
<li><a href="/">帮助</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
{% if request.user.is_authenticated %}
<li class="dropdown">
<a aria-expanded="false" href="#" class="dropdown-toggle" data-toggle="dropdown">{{ request.user.userpfile.nick_name }}<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">个人信息</a></li>
<li><a href="/logout">注销</a></li>
<li><a href="#">修改密码</a></li>
<li><a href="/login">切换用户</a></li>
</ul>
</li>
{% else %}
<li><a href="/login">登录/注册</a></li>
{% endif %}
</ul>
</div>
</div>
</nav>
<div class="container">
<ul class="list-group col-md-3">
{% for group in request.user.userpfile.group.all %}
<li class="list-group-item groupid">
<span class="badge">{{ group.host_set.count }}</span>
{{ group.name }}
</li>
<ul class="list-group">
{% for host in group.host_set.all %}
<li class="list-group-item hostli"><a href="/?hostid={{ host.id }}">{{ host.hostname }}</a></li>
{% endfor %}
</ul>
{% endfor %}
</ul>
<div class="col-md-9">
<table class="table">
<tr>
<th>主机名</th>
<th>IP地址</th>
<th>系统</th>
<th>端口</th>
<th>idc</th>
</tr>
<tr class="text_tr">
<td name="hostname">{{ host_obj.hostname }}</td>
<td name="ip_addr">{{ host_obj.ip_addr }}</td>
<td name="system">{{ host_obj.system }}</td>
<td name="port">{{ host_obj.port }}</td>
<td name="idc">{{ host_obj.idc.name }}</td>
</tr>
</table>
<input type="button" id="bt_edit" value="修改">
<input type="button" id="bt_save" value="保存">
<a href="/del/{{ host_obj.id }}"><input type="button" id="bt_del" value="删除"></a>
</div>
</div>
<script src="{% static 'js/jquery.js' %}"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
<script>
$(function () {
$('.groupid').click(function () {
$(this).next().fadeToggle();
});
$(".hostli a[href='/?hostid={{ host_obj.id }}']").addClass('hostinfor');
$(".hostli a[href='/?hostid={{ host_obj.id }}']").parent().parent().prev().addClass('active')
var firstclick = 0;
$('#bt_edit').click(function () {
if (firstclick == 0) {
$('.text_tr td').each(function () {
var value = $(this).html();
console.log(value);
var html_ele = "<input type='text' value='" + value + "'>";
$(this).html(html_ele)
});//each
}
firstclick = 1;
});// edit
$('#bt_save').click(function () {
host_dict = {
'host_id': '{{ host_obj.id }}'
};
$('.text_tr td input').each(function () {
var value = $(this).val();
var key = $(this).parent().attr('name');
host_dict[key] = value
});//each
console.log(host_dict);
$.get('/host_save', host_dict, function (callback) {
if (callback == 'ok') {
$('.text_tr td input').each(function () {
var value = $(this).val();
$(this).parent().html(value);
});
}//end if
})
});//save
});//end
</script>
</body>
</html>
login.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
{# <meta http-equiv="content-type" content="text/html; charset=UTF-8">#}
{# <meta charset="utf-8">#}
{# <meta http-equiv="X-UA-Compatible" content="IE=edge">#}
{# <meta name="viewport" content="width=device-width, initial-scale=1">#}
{# <meta name="description" content="">#}
{# <meta name="author" content="">#}
{# <title>登录</title>#}
<link rel="stylesheet" href="{% static 'css/signin.css' %}">
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
</head>
<body>
<div class="container">
<form class="form-signin" action="" role="form" method="get">
<h2 class="form-signin-heading">请登录</h2>
<input class="form-control" name="username" placeholder="Username" required="" autofocus="" type="text">
<input class="form-control" name="password" placeholder="Password" required="" type="password">
<div class="checkbox">
<label>
<input value="remember-me" type="checkbox">Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">登录</button>
</form>
</div>
<script src="{% static 'js/ie10-viewport-bug-workaround.js' %}"></script>
</body>
</html>