#templates/app1/login.html
#先写登录页面
<form action = '/app1/login/' method='POST'>
<p><input name = 'username'/></p>
<p><input name = 'password'/></p>
<p><input type = 'submit' value = '提交'/> <label style="color:red;">{{ status }}</label></p>
</form>
#templates/app1/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>欢迎登录老男孩主机管理系统</h1>
<ul>
<li><a href="/app1/user/">用户管理</a></li>
<li><a href="/app1/usergroup/">用户组管理</a></li>
<li><a href="/app1/host/">主机管理</a></li>
</ul>
</body>
</html>
#host.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<div>
<h1>添加主机</h1>
<form action="app1/host",method = 'POST'>
<p><input name ='hostname'/></p>
<p><input name ='ip'/></p>
<p>
<select name="group">
{% for item in group %}
<option value="{{ item.id }}">{{
item.GroupName }}</option>
{% endfor %}
</select>
</p>
<p><input type ='submit' value="添加"/></p>
</form>
</div>
<div>
<h1>主机列表</h1>
<table>
<tr>
<td>主机名</td>
<td>IP</td>
<td>所属用户组</td>
</tr>
</table>
</div>
</body>
</html>
#vews.py
def register(request):
t1 = models.UserTypr.objects.create(name='超级管理员')
t2 = models.UserTypr.objects.create(name='普通管理员')
u1 = models.UserInfo.objects.create(username='alex',passwprd='123',gender='1',email='1@qq.com',user_type=t1)
groupObjA = models.UserGroup.objects.create(GroupName='主机A')
groupObjB = models.UserGroup.objects.create(GroupName='主机B')
groupObjA.user.add(u1)
groupObjB.user.add(u1)
def login(request):
ret = {'status':''}
if request.method == 'post':
username = request.POST.get('username',None)
password = request.POST.get('password',None)
is_auth = all([username,password]) #all函数 判断是否是true
if is_auth:
count = models.UserInfo.objects.filter(username=username,password=password).count()
if count == 1:
return redirect('/app01/index/')
else:
ret['status'] = '用户名和密码不能为空'
return render_to_response('app1/login.html',ret)
def index(request):
return render_to_response('app1/index.html')
def host(request):
ret = {'status':'','data':None,'group':'None'}
usergroup = models.UserGroup.objects.all()
ret['group'] = usergroup
if request.method == 'POST':
hostname = request.POST.get('hostname',None)
ip = request.POST.get('ip',None)
groupId = request.POST.get('group',None)
is_auth = all([hostname,ip])
if is_auth:
groupObj = models.UserGroup.objects.get(id=groupId)
models.Asset.objects.create(hostname=hostname,ip=ip,user_group=groupObj)
else:
ret['status'] = 'hostname或ip不能为空.'
data = models.Asset.objects.all()
ret['data'] = data
return render_to_response('app1/host.html',ret)
#主的urls.py
from django.conf.urls import patterns,include
url(r'^app1/',include('app1.urls'))
#app1/urls.py
from app1 import views
url(r'^login/',views.login)
url(r'^index/',views.index)
url(r'^register/',views.register)
#forms.py
from django import forms
class ALogin(forms.Form):
username = forms.CharField(required=True)
email = forms.EmailField()
ip = forms.GenericIPAddressField()
#app03/views.py
def index(request):
obj = forms.ALogin()
if request.method == 'POST':
checkForm = forms.ALogin(request.POST)
checkResult = checkForm.is_valid()
print(checkResult)
return render_to_response('app03/index.html',{'data':obj})
<h1>Form</h1>
<form action="/app03/login/" method='POST'>
<div>
<div>
{{data.username}}
</div>
<div>
{{data.email}}
</div>
<div>
{{data.ip}}
</div>
</div>
<div>
<input type = 'submit' value="提交"/><label style="color:red">{{ status }}</label>
</div>
</form>
#先写登录页面
<form action = '/app1/login/' method='POST'>
<p><input name = 'username'/></p>
<p><input name = 'password'/></p>
<p><input type = 'submit' value = '提交'/> <label style="color:red;">{{ status }}</label></p>
</form>
#templates/app1/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>欢迎登录老男孩主机管理系统</h1>
<ul>
<li><a href="/app1/user/">用户管理</a></li>
<li><a href="/app1/usergroup/">用户组管理</a></li>
<li><a href="/app1/host/">主机管理</a></li>
</ul>
</body>
</html>
#host.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<div>
<h1>添加主机</h1>
<form action="app1/host",method = 'POST'>
<p><input name ='hostname'/></p>
<p><input name ='ip'/></p>
<p>
<select name="group">
{% for item in group %}
<option value="{{ item.id }}">{{
item.GroupName }}</option>
{% endfor %}
</select>
</p>
<p><input type ='submit' value="添加"/></p>
</form>
</div>
<div>
<h1>主机列表</h1>
<table>
<tr>
<td>主机名</td>
<td>IP</td>
<td>所属用户组</td>
</tr>
</table>
</div>
</body>
</html>
#vews.py
def register(request):
t1 = models.UserTypr.objects.create(name='超级管理员')
t2 = models.UserTypr.objects.create(name='普通管理员')
u1 = models.UserInfo.objects.create(username='alex',passwprd='123',gender='1',email='1@qq.com',user_type=t1)
groupObjA = models.UserGroup.objects.create(GroupName='主机A')
groupObjB = models.UserGroup.objects.create(GroupName='主机B')
groupObjA.user.add(u1)
groupObjB.user.add(u1)
def login(request):
ret = {'status':''}
if request.method == 'post':
username = request.POST.get('username',None)
password = request.POST.get('password',None)
is_auth = all([username,password]) #all函数 判断是否是true
if is_auth:
count = models.UserInfo.objects.filter(username=username,password=password).count()
if count == 1:
return redirect('/app01/index/')
else:
ret['status'] = '用户名和密码不能为空'
return render_to_response('app1/login.html',ret)
def index(request):
return render_to_response('app1/index.html')
def host(request):
ret = {'status':'','data':None,'group':'None'}
usergroup = models.UserGroup.objects.all()
ret['group'] = usergroup
if request.method == 'POST':
hostname = request.POST.get('hostname',None)
ip = request.POST.get('ip',None)
groupId = request.POST.get('group',None)
is_auth = all([hostname,ip])
if is_auth:
groupObj = models.UserGroup.objects.get(id=groupId)
models.Asset.objects.create(hostname=hostname,ip=ip,user_group=groupObj)
else:
ret['status'] = 'hostname或ip不能为空.'
data = models.Asset.objects.all()
ret['data'] = data
return render_to_response('app1/host.html',ret)
#主的urls.py
from django.conf.urls import patterns,include
url(r'^app1/',include('app1.urls'))
#app1/urls.py
from app1 import views
url(r'^login/',views.login)
url(r'^index/',views.index)
url(r'^register/',views.register)
#forms.py
from django import forms
class ALogin(forms.Form):
username = forms.CharField(required=True)
email = forms.EmailField()
ip = forms.GenericIPAddressField()
#app03/views.py
def index(request):
obj = forms.ALogin()
if request.method == 'POST':
checkForm = forms.ALogin(request.POST)
checkResult = checkForm.is_valid()
print(checkResult)
return render_to_response('app03/index.html',{'data':obj})
<h1>Form</h1>
<form action="/app03/login/" method='POST'>
<div>
<div>
{{data.username}}
</div>
<div>
{{data.email}}
</div>
<div>
{{data.ip}}
</div>
</div>
<div>
<input type = 'submit' value="提交"/><label style="color:red">{{ status }}</label>
</div>
</form>