显示用户列表

进入应用myadmin里的urls.py,并增加路径

1
url(r'^user/index/$',UsersViews.user_index,name="myadmin_user_index"),

进入应用myadmin/views/UsersViews,并输入

1
2
3
4
5
6
7
8
9
# 会员列表
def user_index(request):
# 获取所有用户数据
data = Users.objects.all()
#分配数据

context = {'userlist': data}
# 加载模板
return render(request, 'myadmin/users/index.html', context)

进入模板templates/myadmin/users,创建index.html,并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台会员列表</title>
{% endblock %}

{% block con %}
<div class="row-content am-cf">
<div class="row">
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
<div class="widget am-cf">
<div class="widget-head am-cf">
<div class="widget-title am-cf">会员列表</div>
</div>
<div class="widget-body am-fr">
<div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
<div class="am-form-group">
<div class="am-btn-toolbar">
<div class="am-btn-group am-btn-group-xs">
<a href="{% url 'myadmin_user_add' %}" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</a>
</div>
</div>
</div>
</div>
<form>
<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
<div class="am-form-group tpl-table-list-select">
<select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">
<option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>全局搜索</option>
<option value="phone" {% if request.GET.types == 'phone' %} selected {% endif %}>手机号</option>
<option value="id" {% if request.GET.types == 'id' %} selected {% endif %}>ID</option>
<option value="nikename" {% if request.GET.types == 'nikename' %} selected {% endif %}>昵称</option>
<option value="email" {% if request.GET.types == 'email' %} selected {% endif %}>邮箱</option>
</select>
</div>
</div>
<div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
<div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
<input type="text" name="keywords" value="{{ request.GET.keywords }}" class="am-form-field ">
<span class="am-input-group-btn">
<button class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
</span>
</div>
</div>
</form>

<div class="am-u-sm-12">
<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
<thead>
<tr>
<th>ID</th>
<th>用户缩略图</th>
<th>昵称</th>
<th>手机号</th>
<th>邮箱</th>
<th>年龄</th>
<th>性别</th>
<th>状态</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for v in userlist %}
<tr>
<td class="am-text-middle">{{ v.id }}</td>
<td>
<img src="{{ v.pic_url }}" class="tpl-table-line-img" alt="">
</td>
<td class="am-text-middle">{{ v.nikename }}</td>
<td class="am-text-middle">{{ v.phone }}</td>
<td class="am-text-middle">{{ v.email }}</td>
<td class="am-text-middle">{{ v.age }}</td>
<td class="am-text-middle">
{% if v.sex == '0' %}

{% elif v.sex == '1' %}

{% else %}
保密
{% endif%}
</td>
<td class="am-text-middle">
<select uid="{{ v.id }}" class="select-status" style="color: black">
<option value="0" {% if v.status == 0 %} selected {% endif %}>正常</option>
<option value="1" {% if v.status == 1 %} selected {% endif %}>禁用</option>
</select>
</td>
<td class="am-text-middle">{{ v.addtime }}</td>
<td class="am-text-middle">
<div class="tpl-table-black-operation">
<a href="{% url 'myadmin_user_edit' %}?uid={{ v.id }}">
<i class="am-icon-pencil"></i> 编辑
</a>
</div>
</td>
</tr>
{% endfor %}

<!-- more data -->
</tbody>
</table>
</div>
{% comment %} <div class="am-u-lg-12 am-cf">
<div class="am-fr">
<ul class="am-pagination tpl-pagination">
<!-- <li class="am-disabled"><a href="#">«</a></li> -->
<!-- <li class="am-active"><a href="#">1</a></li> -->
{% load pagetag %}

{% showpage userlist.paginator.num_pages request %}
<!-- <li><a href="#">»</a></li> -->
</ul>
</div>
</div>{% endcomment %}
</div>
</div>
</div>
</div>
{% comment %}<script type="text/javascript">
$('.select-status').change(function(){
// 获取当前元素的 值
var status = $(this).val()
// 获取当前元素的 id
var uid = $(this).attr('uid')

// 发送ajax请求.修改状态
$.get('{% url 'myadmin_user_set_status' %}',{'uid':uid,'status':status},function(data){
// 判断当前的返回值
if(data['code'] == 0){
alert(data['msg'])
}
},'json')
})
</script>{% endcomment %}
{% endblock %}

用户列表的编辑

进入应用myadmin里的urls.py,并增加路径

1
url(r'^user/edit/$',UsersViews.user_edit,name="myadmin_user_edit"),

进入应用myadmin/views/UsersViews,并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from django.contrib.auth.hashers import make_password, check_password
from DjangoProject.settings import BASE_DIR
import os
# 会员编辑
def user_edit(request):
# 接受会员id
uid = request.GET.get('uid')
# 获取当前会员对象
ob = Users.objects.get(id=uid)

# 判断当前的请求方式
if request.method == 'POST':
# 判断密码是否更新
if request.POST.get('password',None):
# 更新密码
ob.password = make_password(request.POST['password'], None, 'pbkdf2_sha256')

# 判断头像是否更新
myfile = request.FILES.get('pic',None)
if myfile:
# 如果有新头像上传,则先删除原头像图片,
os.remove(BASE_DIR+ob.pic_url)
# /static/uploads/1545033786.7237682.png
# ./static/uploads/1545033786.7237682.png
# /home/yc/py16/py16-project/web
# 在上传新的头像
ob.pic_url = uploads_pic(myfile)

# 更新其它字段
ob.nikename = request.POST.get('nikename')
ob.email = request.POST.get('email')
ob.phone = request.POST.get('phone')
ob.age = request.POST.get('age')
ob.sex = request.POST.get('sex')
ob.save()
return HttpResponse('<script>alert("更新成功");location.href="/myadmin/user/index/";</script>')


else:
# 显示编辑表单
context = {'uinfo':ob}
return render(request,'myadmin/users/edit.html',context)

进入模板templates/myadmin/users,创建edit.html,并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台会员编辑</title>
{% endblock %}

{% block con %}
<div class="row-content am-cf">
<div class="row">
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
<div class="widget am-cf">
<div class="widget-head am-cf">
<div class="widget-title am-fl">
会员编辑
</div>
<div class="widget-function am-fr">
<a href="javascript:;" class="am-icon-cog">
</a>
</div>
</div>
<div class="widget-body am-fr">
<form action="{% url 'myadmin_user_edit' %}?uid={{ uinfo.id }}" method="POST" enctype="multipart/form-data" class="am-form tpl-form-line-form">
{% csrf_token %}
<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
昵称
</label>
<div class="am-u-sm-9">
<input type="text" name="nikename" value="{{ uinfo.nikename }}" class="tpl-form-input"
placeholder="请输入昵称">
</div>
</div>
<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
密码
</label>
<div class="am-u-sm-9">
<input type="password" name="password" class="tpl-form-input"
placeholder="如果不需要修改密码,则不用填写">
</div>
</div>
<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
手机号
</label>
<div class="am-u-sm-9">
<input type="text" name="phone" value="{{ uinfo.phone }}" class="tpl-form-input"
placeholder="请输入手机号">
</div>
</div>
<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
邮箱
</label>
<div class="am-u-sm-9">
<input type="email" name="email" value="{{ uinfo.email }}" class="tpl-form-input"
placeholder="请输入email">
</div>
</div>

<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
年龄
</label>
<div class="am-u-sm-9">
<input type="number" name="age" value="{{ uinfo.age }}" class="tpl-form-input"
placeholder="请输入年龄">
</div>
</div>

<div class="am-form-group">
<label for="user-name" class="am-u-sm-3 am-form-label">
性别
</label>
<div class="am-u-sm-9">
<label class="am-radio-inline"><input type="radio" value="0" {% if uinfo.sex == '0' %} checked {% endif %} name="sex"> 女</label>
<label class="am-radio-inline"><input type="radio" value="1" {% if uinfo.sex == '1' %} checked {% endif %} name="sex"> 男</label>
</div>
</div>

<div class="am-form-group">
<label for="user-weibo" class="am-u-sm-3 am-form-label">
封面图
<span class="tpl-form-line-small-title">
Images
</span>
</label>
<div class="am-u-sm-9">
<div class="am-form-group am-form-file">
<div class="tpl-form-file-img">
<img src="{{ uinfo.pic_url }}" width="200" alt="">
</div>
<button type="button" class="am-btn am-btn-danger am-btn-sm">
<i class="am-icon-cloud-upload">
</i>
编辑封面图片
</button>
<input id="doc-form-file" name="pic" type="file" multiple="">
</div>
</div>
</div>

<div class="am-form-group">
<div class="am-u-sm-9 am-u-sm-push-3">
<button class="am-btn am-btn-primary tpl-btn-bg-color-success ">
提交
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

地址的使用,分两种情况

一种是系统的文件操作 可以使用 ./static/uploads/ 或者 /home/yc/py16/py16-project/web/static/uploads/

一种是给服务器使用,浏览器访问时使用 /static/uploads/ 因为此处 / 代表当前服务器地址 http://127.0.0.1:8000

用户列表分页、搜索结果分页

进入应用myadmin/views/UsersViews.py,在user_index函数中添加分页功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 导入分页类
from django.core.paginator import Paginator
# 实例化分页类,第二个参数是每页多少条
p = Paginator(data, 6)
# 获取当前的页码数
pageindex = request.GET.get('page', 1)
# 获取当前页的数据
userlist = p.page(pageindex)

# 获取所有的页码
# p.count 一共多少条
# pages = p.page_range #迭代器(range(,))
# pages = p.num_pages #分了多少页

context = {'userlist': userlist}
# 加载模板
return render(request, 'myadmin/users/index.html', context)

进入应用myadmin,创建templatetags文件夹,然后在此文件夹下创建__init__.py和pagetag.py,最后在page.py中输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from django import template
from django.utils.html import format_html

register = template.Library()

# 自定义分页标签
@register.simple_tag
def showpage(count,request):
'''
count 总页数
request 请求对象
'''

# 接受当前的页码数
p = int(request.GET.get('page',1))

# 获取当前请求中所有参数
data = request.GET.dict()
args = ''
for k,v in data.items():
print(k,v)
if k != 'page':
args += '&'+k+'='+v
# {'types': 'all', 'keywords': 'wu','page':2}
# &types=all&keywords=wu
# types=allkeywords=wu
# &types=all&keywords=wu

start = p-5
end = p+4

# 判断 如果当前页 小于 5
if p <= 5:
start = 1
end = 10
# 判断 如果当前页 大于 总页数-5
if p > count-5:
start = count-9
end = count
# 判断 如果总页数 小于 10
if count < 10:
start = 1
end = count

pagehtml = ''
# 首页
pagehtml += '<li><a href="?page=1{args}">首页</a></li>'.format(args=args)
# 上一页
if p > 1:
pagehtml += '<li><a href="?page={p}{args}">上一页</a></li>'.format(p=p-1,args=args)

for x in range(start,end+1):
# 判断是否为当前页
if p == x:
pagehtml += '<li class="am-active"><a href="?page={p}{args}">{p}</a></li>'.format(p=x,args=args)
else:
pagehtml += '<li><a href="?page={p}{args}">{p}</a></li>'.format(p=x,args=args)
# 下一页
if p < count:
pagehtml += '<li><a href="?page={p}{args}">下一页</a></li>'.format(p=p+1,args=args)
# 尾页
pagehtml += '<li><a href="?page={p}{args}">尾页</a></li>'.format(p=count,args=args)

return format_html(pagehtml)
# return pagehtml

进入模板templates/myadmin/users/index.html,把之前隐藏的分页脚标显示出来

用户状态的更新

进入模板templates/myadmin/users/index.html,把$(‘.select-status’)的js代码显示。

进入应用myadmin/urls.py,并添加路径

1
url(r'^user/setstatus/$',UsersViews.user_set_status,name="myadmin_user_set_status"),

进入应用myadmin/views/UsersViews,并添加函数

1
2
3
4
5
6
7
8
from django.http import JsonResponse
# 会员状态的更新
def user_set_status(request):
# 通过uid获取当前会员对象
ob = Users.objects.get(id=request.GET.get('uid'))
ob.status = request.GET.get('status')
ob.save()
return JsonResponse({'msg':'状态更新成功','code':0})