1. 配置数据库链接
1 import pymysql 2 pymysql.install_as_MySQLdb()
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', 4 'NAME': 'computerManage', 5 'USER': 'root', 6 'PASSWORD': '123', 7 'HOST': 'localhost', 8 'PORT': '3306', 9 } 10 }
2. 取消POST提交限制
1 MIDDLEWARE = [ 2 'django.middleware.security.SecurityMiddleware', 3 'django.contrib.sessions.middleware.SessionMiddleware', 4 'django.middleware.common.CommonMiddleware', 5 # 'django.middleware.csrf.CsrfViewMiddleware', 6 'django.contrib.auth.middleware.AuthenticationMiddleware', 7 'django.contrib.messages.middleware.MessageMiddleware', 8 'django.middleware.clickjacking.XFrameOptionsMiddleware', 9 ]
3. 使用静态页模板
1 TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'templates')] 5 , 6 'APP_DIRS': True,
4. 配置静态文件目录
1 STATIC_URL = '/static/' 2 3 STATICFILES_DIRS = ( 4 os.path.join(BASE_DIR, "static"), 5 )
5. 配置路由
1 from django.conf.urls import url,include 2 from django.contrib import admin 3 import web.urls 4 from django.shortcuts import render_to_response 5 6 def index(request): 7 return render_to_response('index.html') 8 9 urlpatterns = [ 10 url(r'^$', index), 11 url(r'^admin/', admin.site.urls), 12 url(r'^web/', include('web.urls')), 13 ]
1 from django.conf.urls import url 2 from web.views import * 3 urlpatterns = [ 4 url(r'^pcadd/',pcAdd), 5 url(r'^pcedit/(?P<id>d+)',pcEdit), 6 url(r'^pclist/',pcList), 7 url(r'^grouplist/',groupList), 8 url(r'^groupadd/',groupAdd), 9 url(r'^groupManagePcs/',groupManagePcs), 10 url(r'^utypelist/',userTypeList), 11 url(r'^utypeadd/',userTypeAdd), 12 url(r'^userlist/',userList), 13 url(r'^useradd/',userAdd), 14 ]
6. 模板文件
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> 7 <title>{% block title %}{% endblock %}</title> 8 <link rel="stylesheet" href="/static/css/bootstrap.min.css"> 9 <script type="text/javascript" src="/static/js/jquery.js"></script> 10 <script type="text/javascript"> 11 $(function () { 12 var curUrl = window.location.pathname; 13 $("#topNav li").each(function(){ 14 if(curUrl.indexOf($(this).attr("nav"))>-1){ 15 $(this).addClass('active'); 16 }else{ 17 $(this).removeClass('active'); 18 } 19 }); 20 }); 21 </script> 22 {% block headjs %}{% endblock %} 23 </head> 24 <body> 25 <div class="container" style="margin-bottom: 5px;"> 26 <ul id="topNav" class="nav nav-tabs" role="tablist" style="background-color: #efefff"> 27 <li role="presentation" nav="index" style='font-weight: bold;'><a href="/">电脑管理系统</a></li> 28 <li role="presentation" nav="/web/pc"><a href="/web/pclist/">电脑管理</a></li> 29 <li role="presentation" nav="/web/group"><a href="/web/grouplist/">用户组管理</a></li> 30 <li role="presentation" nav="/web/utype"><a href="/web/utypelist/">用户类型管理</a></li> 31 <li role="presentation" nav="/web/user"><a href="/web/userlist/">用户管理</a></li> 32 </ul> 33 </div> 34 {% block content %}{% endblock %} 35 </body> 36 </html>
1 {% extends "base.html" %} 2 {% block title %}录入电脑信息{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <form action="{% if id %}/web/pcedit/{{ id }}{% else %}/web/pcadd/{% endif %}" method="post" class="form-horizontal"> 6 <div class="form-group"> 7 <h3 class="col-md-5 text-center">录入电脑信息</h3> 8 </div> 9 <div class="form-group"> 10 <label class="control-label col-md-2">名称:</label> 11 <div class="col-md-3">{{ form.name }}</div> 12 </div> 13 <div class="form-group"> 14 <label class="control-label col-md-2">编号:</label> 15 <div class="col-md-3">{{ form.code }}</div> 16 </div> 17 <div class="form-group"> 18 <label class="control-label col-md-2">IP:</label> 19 <div class="col-md-3">{{ form.ip }}</div> 20 </div> 21 <div class="form-group"> 22 <div class="col-md-6 text-center"> 23 <button type="submit" class="btn btn-default" name="act" value="save">保存</button> 24 {% if id %} 25 <button type="button" class="btn btn-default" onclick="window.close()" style="margin-left: 20px">关闭</button> 26 {% else %} 27 <button type="submit" class="btn btn-default" name="act" value="add" style="margin-left: 20px">添加</button> 28 {% endif %} 29 </div> 30 </div> 31 <div class="col-md-6 text-center{% if err %} has-error{% endif %}{% if success %} has-success{% endif %}"> 32 <label class="control-label">{{ err }}{{ success }}</label> 33 </div> 34 </form> 35 </div> 36 {% endblock %}
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首页</title> 6 <script type="text/javascript"> 7 window.location.href="/web/pclist/"; 8 </script> 9 </head> 10 <body> 11 12 </body> 13 </html>
1 {% extends "base.html" %} 2 {% block title %}用户添加{% endblock %} 3 {% block headjs %} 4 <script type="text/javascript"> 5 $(function(){ 6 $("input[name='groupId']").bind("click",function(){ 7 var arr_ords, arr_name; 8 $.ajax({ 9 type:'POST', 10 url:"/web/groupManagePcs/", 11 async:false, 12 data:{ord:$(this).val()}, 13 success: function(r){ 14 if(r!=""){ 15 var arr_ret = r.split('1') 16 var pcords = arr_ret[0]; 17 var pcnames = arr_ret[1]; 18 if(pcords!=""){ 19 arr_ords = pcords.split(","); 20 arr_name = pcnames.split("2"); 21 } 22 } 23 } 24 }); 25 26 if($(this).attr("checked")){ 27 for(var i=0;i<arr_ords.length;i++){ 28 if($("#pc_"+arr_ords[i]).size()==0){ 29 $('#pcShow').append('<label class="checkbox-inline"><input type="checkbox" name="pcord" id="pc_'+arr_ords[i]+'" value="'+arr_ords[i]+'">'+arr_name[i]+'</label>'); 30 } 31 } 32 }else{ 33 for(var i=0;i<arr_ords.length;i++){ 34 if($("#pc_"+arr_ords[i]).size()>0){ 35 $('#pc_'+arr_ords[i]).parent().remove() 36 } 37 } 38 } 39 }); 40 } ); 41 </script> 42 {% endblock %} 43 {% block content %} 44 <div class="container"> 45 <form action="/web/useradd/" method="post" class="form-horizontal"> 46 <div class="form-group"> 47 <h3 class="col-md-5 text-center">录入用户信息</h3> 48 </div> 49 <div class="form-group"> 50 <label class="control-label col-md-2">名称:</label> 51 <div class="col-md-3"><input type="text" class="form-control" name="name"></div> 52 </div> 53 <div class="form-group"> 54 <label class="control-label col-md-2">账号:</label> 55 <div class="col-md-3"><input type="text" class="form-control" name="username"></div> 56 </div> 57 <div class="form-group"> 58 <label class="control-label col-md-2">密码:</label> 59 <div class="col-md-3"><input type="password" class="form-control" name="password"></div> 60 </div> 61 <div class="form-group"> 62 <label class="control-label col-md-2">所属类型:</label> 63 <div class="col-md-3"> 64 <select type="text" class="form-control" name="typeId"> 65 <option value="">请选择用户类型</option> 66 {% for utype in utlist %} 67 <option value="{{ utype.id }}">{{ utype.name }}</option> 68 {% endfor %} 69 </select> 70 </div> 71 </div> 72 <div class="form-group"> 73 <label class="control-label col-md-2">所属组:</label> 74 <div class="col-md-5"> 75 {% for group in grouplist %} 76 <label class="checkbox-inline"><input type="checkbox" name="groupId" value="{{ group.id }}">{{ group.name }}</label> 77 {% endfor %} 78 </div> 79 </div> 80 <div class="form-group"> 81 <label class="control-label col-md-2">可管理电脑:</label> 82 <div class="col-md-5" id="pcShow"></div> 83 </div> 84 <div class="form-group"> 85 <label class="control-label col-md-2">年龄:</label> 86 <div class="col-md-1"> <input type="text" class="form-control" name="age"></div> 87 <label class="control-label col-md-1">性别:</label> 88 <div class="col-md-2"> 89 <label class="radio-inline"><input type="radio" name="gender" value="0" checked="">男</label> 90 <label class="radio-inline"><input type="radio" name="gender" value="1">女</label> 91 </div> 92 </div> 93 <div class="form-group"> 94 <label class="control-label col-md-2">手机:</label> 95 <div class="col-md-3"><input type="text" class="form-control" name="phone"></div> 96 </div> 97 <div class="form-group"> 98 <label class="control-label col-md-2">邮箱:</label> 99 <div class="col-md-3"><input type="text" class="form-control" name="email"></div> 100 </div> 101 <div class="form-group"> 102 <label class="control-label col-md-2">备注:</label> 103 <div class="col-md-5"><textarea class="form-control" name="memo"></textarea></div> 104 </div> 105 <div class="form-group"> 106 <div class="col-md-6 text-center"> 107 <button type="submit" class="btn btn-default" name="act" value="save">保存</button> 108 <button type="submit" class="btn btn-default" name="act" value="add" style="margin-left: 20px">添加</button> 109 </div> 110 </div> 111 <div class="col-md-6 text-center{% if err %} has-error{% endif %}{% if success %} has-success{% endif %}"> 112 <label class="control-label">{{ err }}{{ success }}</label> 113 </div> 114 </form> 115 </div> 116 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}添加用户组{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <form action="/web/groupadd/" method="post" class="form-horizontal"> 6 <div class="form-group"> 7 <h3 class="col-md-5 text-center">添加用户组</h3> 8 </div> 9 <div class="form-group"> 10 <label class="control-label col-md-2">名称:</label> 11 <div class="col-md-3"><input type="text" class="form-control" name="name"></div> 12 </div> 13 <div class="form-group"> 14 <label class="control-label col-md-2">可管理电脑:</label> 15 <div class="col-md-5"> 16 {% for pc in pclist %} 17 <label class="checkbox-inline"><input type="checkbox" name="pc" value="{{ pc.id }}">{{ pc.name }}</label> 18 {% endfor %} 19 </div> 20 </div> 21 <div class="form-group"> 22 <div class="col-md-6 text-center"> 23 <button type="submit" class="btn btn-default" name="act" value="save">保存</button> 24 <button type="submit" class="btn btn-default" name="act" value="add" style="margin-left: 20px">添加</button> 25 </div> 26 </div> 27 <div class="col-md-6 text-center{% if err %} has-error{% endif %}{% if success %} has-success{% endif %}"> 28 <label class="control-label">{{ err }}{{ success }}</label> 29 </div> 30 </form> 31 </div> 32 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}用户组列表{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <div class="col-md-12 text-right" style="margin-bottom: 8px;"> 6 <a class="btn btn-primary" href="/web/groupadd/">新增用户组</a> 7 </div> 8 <div class="col-md-12"> 9 <table class="table table-bordered table-striped table-hover"> 10 <tr class="info"> 11 <th>名称</th> 12 <th>可管理电脑</th> 13 <th class="text-center">操作</th> 14 </tr> 15 {% for item in gplist %} 16 <tr> 17 <td>{{ item.group.name }}</td> 18 <td>{{ item.managePcs }}</td> 19 <td class="text-center"></td> 20 </tr> 21 {% endfor %} 22 </table> 23 </div> 24 </div> 25 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}用户列表{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <div class="col-md-12 text-right" style="margin-bottom: 5px;"> 6 <a class="btn btn-primary" href="/web/useradd/">新增用户</a> 7 </div> 8 <div class="col-md-12"> 9 <table class="table table-bordered table-striped table-hover"> 10 <tr class="info"> 11 <th>名称</th> 12 <th>用户名</th> 13 <th>用户组</th> 14 <th class="text-center">类型</th> 15 <th class="text-center">年龄</th> 16 <th>手机</th> 17 <th>邮箱</th> 18 <th class="text-center">添加时间</th> 19 <th class="text-center">操作</th> 20 </tr> 21 {% for user in list %} 22 <tr> 23 <td>{{ user.name }}</td> 24 <td>{{ user.username }}</td> 25 <td>{{ user.groupId.name }}</td> 26 <td class="text-center">{{ user.typeId.name }}</td> 27 <td class="text-center">{% if user.gender %}女{% else %}男{% endif %}</td> 28 <td>{{ user.phone }}</td> 29 <td>{{ user.email }}</td> 30 <td class="text-center">{{ user.createDate|date:"Y-m-d H:i:s" }}</td> 31 <td class="text-center"></td> 32 </tr> 33 {% endfor %} 34 </table> 35 </div> 36 </div> 37 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}添加用户类型{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <form action="{% if id %}}/web/utypedit/{{ id }}{% else %}/web/utypeadd/{% endif %}" method="post" class="form-horizontal"> 6 <div class="form-group"> 7 <h3 class="col-md-5 text-center">添加用户类型</h3> 8 </div> 9 <div class="form-group"> 10 <label class="control-label col-md-2">类型名称:</label> 11 <div class="col-md-3">{{ form.name}}</div> 12 </div> 13 <div class="form-group"> 14 <div class="col-md-6 text-center"> 15 <button type="submit" class="btn btn-default" name="act" value="save">保存</button> 16 {% if id %}} 17 <button type="submit" class="btn btn-default" name="act" value="add" style="margin-left: 20px">添加</button> 18 {% else %} 19 <button type="submit" class="btn btn-default" name="act" value="add" style="margin-left: 20px">添加</button> 20 {% endif %} 21 </div> 22 </div> 23 <div class="col-md-6 text-center{% if err %} has-error{% endif %}{% if success %} has-success{% endif %}"> 24 <label class="control-label">{{ err }}{{ success }}</label> 25 </div> 26 </form> 27 </div> 28 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}用户类型列表{% endblock %} 3 {% block content %} 4 <div class="container"> 5 <div class="col-md-12 text-right" style="margin-bottom: 8px;"> 6 <a class="btn btn-primary" href="/web/utypeadd/">新增用户类型</a> 7 </div> 8 <div class="col-md-12"> 9 <table class="table table-bordered table-striped table-hover"> 10 <tr class="info"> 11 <th>类型名称</th> 12 <th class="text-center">操作</th> 13 </tr> 14 {% for item in list %} 15 <tr> 16 <td>{{ item.name }}</td> 17 <td class="text-center"></td> 18 </tr> 19 {% endfor %} 20 </table> 21 </div> 22 </div> 23 {% endblock %}
1 {% extends "base.html" %} 2 {% block title %}电脑列表{% endblock %} 3 {% block headjs %} 4 <script type="text/javascript"> 5 function edit(id){ 6 window.open('/web/pcedit/'+id, 'newwindow', 'height=500, width=1000, top=100, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') 7 } 8 </script> 9 {% endblock %} 10 {% block content %} 11 <div class="container"> 12 <div class="col-md-12 text-right" style="margin-bottom: 5px;"> 13 <a class="btn btn-primary" href="/web/pcadd/">新增电脑</a> 14 </div> 15 <div class="col-md-12"> 16 <table class="table table-bordered table-striped table-hover"> 17 <tr class="info"> 18 <th>名称</th> 19 <th>编号</th> 20 <th>IP</th> 21 <th class="text-center">操作</th> 22 </tr> 23 {% for pc in list %} 24 <tr> 25 <td>{{ pc.name }}</td> 26 <td>{{ pc.code }}</td> 27 <td>{{ pc.ip }}</td> 28 <td class="text-center"> 29 <button class="btn btn-default btn-sm" onclick="edit({{ pc.id }})">修改</button> 30 </td> 31 </tr> 32 {% endfor %} 33 </table> 34 </div> 35 <nav class="text-center"> 36 <ul class="pagination">{{ pageHtml }}</ul> 37 </nav> 38 </div> 39 {% endblock %}
7. 使用到的模型
1 from django.db import models 2 3 # 电脑 4 class Computer(models.Model): 5 name = models.CharField(max_length=20) 6 code = models.CharField(max_length=30) 7 ip = models.CharField(null=True, max_length=30) 8 9 # 用户组 10 class UserGroup(models.Model): 11 name = models.CharField(max_length=20) 12 managePcs = models.CharField(null=True,max_length=500) 13 14 # 用户类型 15 class UserType(models.Model): 16 name = models.CharField(max_length=20) 17 18 # 用户 19 class Users(models.Model): 20 name = models.CharField(max_length=20) 21 username = models.CharField(max_length=20) 22 password = models.CharField(max_length=50) 23 email = models.EmailField(max_length=200, null=True) 24 phone = models.CharField(max_length=30, null=True) 25 gender = models.BooleanField(default=False) 26 age = models.IntegerField(null=True) 27 memo = models.TextField(null=True) 28 createDate = models.DateTimeField(auto_now_add=True) 29 updateDate = models.DateTimeField(auto_now=True) 30 typeId = models.ForeignKey(UserType) 31 groupId = models.ManyToManyField(UserGroup) 32 managePcs = models.CharField(null=True,max_length=500)
8. 使用到的表单
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 from django import forms 4 5 class ComputerForm(forms.Form): 6 name = forms.CharField(max_length=20) 7 code = forms.CharField(max_length=30) 8 # code = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'class': 'form-control'})) 9 ip = forms.GenericIPAddressField() 10 def __init__(self,*args,**kwargs): 11 super(ComputerForm,self).__init__(*args,**kwargs) 12 self.fields['name'] = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'})) 13 self.fields['code'] = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'})) 14 self.fields['ip'] = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
1 #/usr/bin/python 2 # -*- coding:utf-8 -*- 3 4 from django import forms 5 6 # class UserAdd(forms.Form): 7 # name = forms.CharField(max_length=20) 8 # username = forms.CharField(max_length=20) 9 # password = forms.CharField(max_length=20) 10 # email = forms.EmailField(max_length=200, null=True) 11 # phone = forms.CharField(max_length=30, null=True) 12 # gender = forms.BooleanField(default=False) 13 # age = forms.IntegerField(null=True) 14 # memo = forms.TextField(null=True) 15 16 17 class UserTypeForm(forms.Form): 18 name = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'class': 'form-control'})) 19
9. 使用到的视图
1 from django.http import HttpResponse, HttpResponseRedirect 2 from django.shortcuts import render, render_to_response 3 from django.template import RequestContext 4 5 from web.Forms.Computer import ComputerForm as pcForm 6 from web.Forms.Users import UserTypeForm 7 from web.models import * 8 from common import PageHelper 9 10 def pcAdd(request): 11 pcform = pcForm() 12 if request.method == 'POST': 13 form = pcForm(request.POST) 14 if form.is_valid(): 15 data = form.cleaned_data 16 if Computer.objects.filter(code=data['code']).count()==0: 17 Computer.objects.create(name=data['name'], code=data['code'], ip=data['ip']) 18 if request.POST['act'] == 'save': 19 # return render_to_response('computerAdd.html', {'form': pcform,'success':'保存成功!'}) 20 return HttpResponseRedirect('/web/pclist/') 21 else: 22 return HttpResponseRedirect('/web/pcadd/') 23 else: 24 return render_to_response('computerAdd.html', {'form': pcform, 'err': '编号为【'+data['code']+'】的电脑已存在!'}) 25 else: 26 return render_to_response('computerAdd.html', {'form': pcform,'err':'保存失败!'}) 27 else: 28 return render_to_response('computerAdd.html',{'form': pcform}) 29 30 def pcEdit(request, id): 31 pcform = pcForm() 32 if request.method == 'POST': 33 form = pcForm(request.POST) 34 if form.is_valid(): 35 data = form.cleaned_data 36 if Computer.objects.filter(id=id).count()>0: 37 pc = Computer.objects.get(id=id) 38 pc.name = data['name'] 39 pc.code = data['code'] 40 pc.ip = data['ip'] 41 pc.save() 42 return HttpResponse('<script>if(window.opener){window.opener.location.reload();window.close();}</script>') 43 else: 44 return render_to_response('computerAdd.html', {'form': pcform,'err':'保存失败!'}) 45 else: 46 # print(id) 47 pc = Computer.objects.get(id=id) 48 pcform = pcForm(initial={'name':pc.name,'code':pc.code,'ip':pc.ip}) 49 return render_to_response('computerAdd.html',{'form': pcform,'id':id}) 50 51 def pcList(request, page): 52 container = {} 53 54 count = Computer.objects.order_by('-id').count() 55 pager = PageHelper.PageHelper('/web/pclist/', count, page,5) 56 list = Computer.objects.order_by('-id')[pager.start:pager.end] 57 58 pageHtml = pager.pageHtml() 59 container['list'] = list 60 container['pageHtml'] = pageHtml 61 return render_to_response('computerList.html', container) 62 63 def transIntOrds(list): 64 ret = '' 65 for ord in list: 66 ret += ('' if ret=='' else ',') + ord 67 return ret 68 69 def getPcListByIds(ords): 70 pcords = list(eval('('+ ords +',)')) 71 pclist = Computer.objects.filter(id__in=pcords) 72 return pclist 73 74 def groupList(request): 75 gplist = [] 76 groups = UserGroup.objects.order_by('-id') 77 for group in groups: 78 pclist = getPcListByIds(group.managePcs) 79 pcnames = "" 80 for pc in pclist: 81 pcnames += pc.name + " " 82 gplist.append({"group":group, "managePcs":pcnames}) 83 return render_to_response('userGroupList.html',{'gplist':gplist}) 84 85 def groupAdd(request): 86 if request.method == 'POST': 87 name = request.POST.get('name','') 88 if name.strip() == '': 89 pclist = Computer.objects.order_by('-id').values('id','name') 90 return render_to_response('userGroupAdd.html',{'pclist':pclist, 'err':'请输入组名称!'}) 91 pc = request.POST.getlist('pc') 92 pcords = transIntOrds(pc) 93 94 if UserGroup.objects.filter(name=name).count()==0: 95 UserGroup.objects.create(name=name, managePcs=pcords) 96 if request.POST['act'] == 'save': 97 return HttpResponseRedirect('/web/grouplist/') 98 else: 99 return HttpResponseRedirect('/web/groupadd') 100 else: 101 pclist = Computer.objects.order_by('-id').values('id','name') 102 return render_to_response('userGroupAdd.html',{'pclist':pclist, 'err':'这个用户组已存在!'}) 103 else: 104 pclist = Computer.objects.order_by('-id').values('id','name') 105 return render_to_response('userGroupAdd.html',{'pclist':pclist}) 106 107 def groupManagePcs(request): 108 ord = request.POST.get('ord',None) 109 if ord==None: 110 ord = 0 111 group = UserGroup.objects.get(id=ord) 112 pclist = getPcListByIds(group.managePcs) 113 pcnames = '' 114 for pc in pclist: 115 pcnames += ('' if pcnames=='' else '2') + pc.name 116 return HttpResponse(group.managePcs +'1'+pcnames) 117 118 def userTypeList(request): 119 utlist = UserType.objects.order_by('-id') 120 return render_to_response('userTypeList.html',{'list':utlist}) 121 122 123 def userTypeAdd(request): 124 utypeForm = UserTypeForm() 125 if request.method == 'POST': 126 form = UserTypeForm(request.POST) 127 if form.is_valid(): 128 data = form.cleaned_data 129 UserType.objects.create(name=data['name']) 130 if request.POST['act'] == 'save': 131 return HttpResponseRedirect('/web/utypelist/') 132 else: 133 return HttpResponseRedirect('/web/utypeadd/') 134 else: 135 return render_to_response('userTypeAdd.html',{'form':utypeForm, 'err':'保存失败!'}) 136 else: 137 return render_to_response('userTypeAdd.html',{'form':utypeForm}) 138 139 140 def userList(request): 141 userList = Users.objects.order_by('-createDate') 142 return render_to_response('userList.html',{'list':userList}) 143 144 def userAdd(request): 145 contaions = {} 146 if request.method == 'POST': 147 noErr = True 148 errTip = '' 149 name = request.POST.get('name',None) 150 username = request.POST.get('username',None) 151 password = request.POST.get('password',None) 152 typeId = request.POST.get('typeId',None) 153 gplist = request.POST.getlist('groupId') 154 pcordlist = request.POST.getlist('pcord') 155 age = request.POST.get('age',None) 156 gender = request.POST.get('gender',None) 157 phone = request.POST.get('phone',None) 158 email = request.POST.get('email',None) 159 memo = request.POST.get('memo',None) 160 act = request.POST.get('act','') 161 if noErr and name == None: 162 errTip = '请输入用户名称!' 163 noErr = False 164 if noErr and username == None: 165 errTip = '请输入用户账号!' 166 noErr = False 167 if noErr and password == None: 168 errTip = '请输入用户密码!' 169 noErr = False 170 if noErr and typeId == None: 171 errTip = '请选择用户类型!' 172 noErr = False 173 if noErr and age!=None and age.isdigit()==False: 174 errTip = '请正确的年龄!' 175 noErr = False 176 if noErr==False: 177 contaions['err'] = errTip 178 grouplist = UserGroup.objects.order_by('-id') 179 contaions['grouplist'] = grouplist 180 utlist = UserType.objects.order_by('-id') 181 contaions['utlist'] = utlist 182 return render_to_response('userAdd.html', contaions) 183 else: 184 user = Users.objects.create(name=name, 185 username=username,password=password, 186 typeId=UserType.objects.get(id=typeId), 187 managePcs=transIntOrds(pcordlist), 188 age=age,gender=gender,phone=phone, 189 email=email,memo=memo) 190 user.save() 191 glist = UserGroup.objects.filter(id__in=gplist) 192 user.groupId.add(*glist) 193 # for gord in gplist: 194 # group = UserGroup.objects.get(id=gord) 195 # user.groupId.add(group) 196 197 if act == 'save': 198 return HttpResponseRedirect('/web/userlist/') 199 else: 200 return HttpResponseRedirect('/web/useradd/') 201 else: 202 grouplist = UserGroup.objects.order_by('-id') 203 contaions['grouplist'] = grouplist 204 utlist = UserType.objects.order_by('-id') 205 contaions['utlist'] = utlist 206 return render_to_response('userAdd.html', contaions)