首页 > 代码库 > Django 电脑使用管理

Django 电脑使用管理

1. 配置数据库链接

技术分享
1 import pymysql
2 pymysql.install_as_MySQLdb()
computerManage/__init__.py
技术分享
 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 }
computerManage/settings.py

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 ]
computer/settings.py

3. 使用静态页模板

技术分享
1 TEMPLATES = [
2     {
3         BACKEND: django.template.backends.django.DjangoTemplates,
4         DIRS: [os.path.join(BASE_DIR, templates)]
5         ,
6         APP_DIRS: True,
computerManage/settings.py

4. 配置静态文件目录

技术分享
1 STATIC_URL = /static/
2 
3 STATICFILES_DIRS = (
4     os.path.join(BASE_DIR, "static"),
5 )
computerManage/settings.py

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 ]
computerManage/urls.py
技术分享
 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 ]
/web/urls.py

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>
templates/base.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 %}
templates/computerAdd.html
技术分享
 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 </div>
36 {% endblock %}
template/computerList.html
技术分享
 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>
templates/index.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="http://www.mamicode.com/+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 %}
templates/userAdd.html
技术分享
 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 %}
templates/userGroupAdd.html
技术分享
 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 %}
templates/userGroupList.html
技术分享
 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 %}
templates/userList.html
技术分享
 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 %}
template/userTypteAdd.html
技术分享
 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 %}
templates/userTypeList

 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)
web/models.py

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}))
web/Forms/Computer.py
技术分享
 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     
web/Forms/Users.py

9. 使用到的视图

技术分享
  1 from django.http import HttpResponse, HttpResponseRedirect
  2 from django.shortcuts import render, render_to_response
  3 from web.Forms.Computer import ComputerForm as pcForm
  4 from web.Forms.Users import UserTypeForm
  5 from web.models import *
  6 
  7 def pcAdd(request):
  8     pcform = pcForm()
  9     if request.method == POST:
 10         form = pcForm(request.POST)
 11         if form.is_valid():
 12             data =http://www.mamicode.com/ form.cleaned_data
 13             if Computer.objects.filter(code=data[code]).count()==0:
 14                 Computer.objects.create(name=data[name], code=data[code], ip=data[ip])
 15                 if request.POST[act] == save:
 16                     # return render_to_response(‘computerAdd.html‘, {‘form‘: pcform,‘success‘:‘保存成功!‘})
 17                     return HttpResponseRedirect(/web/pclist/)
 18                 else:
 19                     return HttpResponseRedirect(/web/pcadd/)
 20             else:
 21                 return render_to_response(computerAdd.html, {form: pcform, err: 编号为【+data[code]+】的电脑已存在!})
 22         else:
 23             return render_to_response(computerAdd.html, {form: pcform,err:保存失败!})
 24     else:
 25         return render_to_response(computerAdd.html,{form: pcform})
 26 
 27 def pcEdit(request, id):
 28     pcform = pcForm()
 29     if request.method == POST:
 30         form = pcForm(request.POST)
 31         if form.is_valid():
 32             data =http://www.mamicode.com/ form.cleaned_data
 33             if Computer.objects.filter(id=id).count()>0:
 34                 pc = Computer.objects.get(id=id)
 35                 pc.name = data[name]
 36                 pc.code = data[code]
 37                 pc.ip = data[ip]
 38                 pc.save()
 39                 return HttpResponse(<script>if(window.opener){window.opener.location.reload();window.close();}</script>)
 40         else:
 41             return render_to_response(computerAdd.html, {form: pcform,err:保存失败!})
 42     else:
 43         # print(id)
 44         pc = Computer.objects.get(id=id)
 45         pcform = pcForm(initial={name:pc.name,code:pc.code,ip:pc.ip})
 46         return render_to_response(computerAdd.html,{form: pcform,id:id})
 47 
 48 def pcList(request):
 49     list = Computer.objects.order_by(-id)
 50     return render_to_response(computerList.html, {list: list})
 51 
 52 def transIntOrds(list):
 53     ret = ‘‘
 54     for ord in list:
 55         ret += (‘‘ if ret==‘‘ else ,) + ord
 56     return ret
 57 
 58 def getPcListByIds(ords):
 59     pcords = list(eval((+ ords +,)))
 60     pclist = Computer.objects.filter(id__in=pcords)
 61     return pclist
 62 
 63 def groupList(request):
 64     gplist = []
 65     groups = UserGroup.objects.order_by(-id)
 66     for group in groups:        
 67         pclist = getPcListByIds(group.managePcs)
 68         pcnames = ""
 69         for pc in pclist:
 70             pcnames += pc.name + " "
 71         gplist.append({"group":group, "managePcs":pcnames})
 72     return render_to_response(userGroupList.html,{gplist:gplist})
 73 
 74 def groupAdd(request):
 75     if request.method == POST:
 76         name = request.POST.get(name,‘‘)
 77         if name.strip() == ‘‘:
 78             pclist = Computer.objects.order_by(-id).values(id,name)
 79             return render_to_response(userGroupAdd.html,{pclist:pclist, err:请输入组名称!})
 80         pc = request.POST.getlist(pc) 
 81         pcords = transIntOrds(pc)        
 82          
 83         if UserGroup.objects.filter(name=name).count()==0:
 84             UserGroup.objects.create(name=name, managePcs=pcords)
 85             if request.POST[act] == save:
 86                 return HttpResponseRedirect(/web/grouplist/)
 87             else:
 88                 return HttpResponseRedirect(/web/groupadd)
 89         else:
 90             pclist = Computer.objects.order_by(-id).values(id,name)
 91             return render_to_response(userGroupAdd.html,{pclist:pclist, err:这个用户组已存在!})
 92     else:
 93         pclist = Computer.objects.order_by(-id).values(id,name)
 94         return render_to_response(userGroupAdd.html,{pclist:pclist})
 95 
 96 def groupManagePcs(request):
 97     ord = request.POST.get(ord,None)
 98     if ord==None:
 99         ord = 0
100     group = UserGroup.objects.get(id=ord)
101     pclist = getPcListByIds(group.managePcs)
102     pcnames = ‘‘
103     for pc in pclist:
104         pcnames += (‘‘ if pcnames==‘‘ else \2) + pc.name 
105     return HttpResponse(group.managePcs +\1+pcnames)
106 
107 def userTypeList(request):
108     utlist = UserType.objects.order_by(-id)
109     return render_to_response(userTypeList.html,{list:utlist})
110 
111 
112 def userTypeAdd(request):
113     utypeForm = UserTypeForm()
114     if request.method == POST:
115         form = UserTypeForm(request.POST)
116         if form.is_valid():
117             data =http://www.mamicode.com/ form.cleaned_data
118             UserType.objects.create(name=data[name])
119             if request.POST[act] == save:
120                 return HttpResponseRedirect(/web/utypelist/)
121             else:
122                 return HttpResponseRedirect(/web/utypeadd/)
123         else:
124             return render_to_response(userTypeAdd.html,{err:保存失败!})        
125     else:        
126         return render_to_response(userTypeAdd.html,{form:utypeForm})
127 
128 
129 def userList(request):
130     userList = Users.objects.order_by(-createDate)
131     return render_to_response(userList.html,{list:userList})
132 
133 def userAdd(request):    
134     contaions = {}
135     if request.method == POST:
136         noErr = True
137         errTip = ‘‘
138         name = request.POST.get(name,None)
139         username = request.POST.get(username,None)
140         password = request.POST.get(password,None)
141         typeId = request.POST.get(typeId,None)
142         gplist = request.POST.getlist(groupId)
143         pcordlist = request.POST.getlist(pcord)
144         age = request.POST.get(age,None)
145         gender = request.POST.get(gender,None)
146         phone = request.POST.get(phone,None)
147         email = request.POST.get(email,None)
148         memo = request.POST.get(memo,None)
149         act = request.POST.get(act,‘‘)
150         if noErr and name == None:
151             errTip = 请输入用户名称!
152             noErr = False
153         if noErr and username == None:
154             errTip = 请输入用户账号!
155             noErr = False
156         if noErr and password == None:
157             errTip = 请输入用户密码!
158             noErr = False
159         if noErr and typeId == None:
160             errTip = 请选择用户类型!
161             noErr = False
162         if noErr and age!=None and age.isdigit()==False:
163             errTip = 请正确的年龄!
164             noErr = False
165         if noErr==False:
166             contaions[err] = errTip
167             grouplist = UserGroup.objects.order_by(-id)
168             contaions[grouplist] = grouplist
169             utlist = UserType.objects.order_by(-id)
170             contaions[utlist] = utlist
171             return render_to_response(userAdd.html, contaions)
172         else:
173             user = Users.objects.create(name=name,
174                 username=username,password=password,
175                 typeId=UserType.objects.get(id=typeId),
176                 managePcs=transIntOrds(pcordlist),
177                 age=age,gender=gender,phone=phone,
178                 email=email,memo=memo)
179             user.save()
180             glist = UserGroup.objects.filter(id__in=gplist)
181             user.groupId.add(*glist)
182             # for gord in gplist:
183             #     group = UserGroup.objects.get(id=gord)
184             #     user.groupId.add(group)
185 
186             if act == save:
187                 return HttpResponseRedirect(/web/userlist/)
188             else:
189                 return HttpResponseRedirect(/web/useradd/)
190     else:
191         grouplist = UserGroup.objects.order_by(-id)
192         contaions[grouplist] = grouplist
193         utlist = UserType.objects.order_by(-id)
194         contaions[utlist] = utlist
195         return render_to_response(userAdd.html, contaions)
web/views.py

 

Django 电脑使用管理