Python3项目初始化2-->用户删除和编辑
作者:互联网
Python3项目初始化2-->用户删除和编辑
8、用户删除
当前用户是:{{ request.session.user.name }}
index.html里面新增如下内容,
当前登录用户是:{{ request.session.user.name }}, <a href="/user/logout/">退出
这里需要model.py里面返回,故model.py里面做调整。
def valid_login(name, password):
users = get_users()
for uid, user in users.items():
if user['name'] == name and user['password'] == password:
user['id'] = uid
return user # 将True 修改为返回值user
return None
首页设置位置,编辑和删除(这里的判断是说不能删除用户自己,uid参数)页面。
<th>联系方式</th>
<th>操作</th
{% for uid, user in users.items %}
<td>{{ user.tel }}</td>
<td>
<a href="{% url 'user:view' %}?uid={{ uid }}">编辑</a>
{% if request.session.user.id != uid %}
<a href="{% url 'user:delete' %}?uid={{ uid }}">删除</a>
{% endif %}
</td>
添加url信息,
path('delete/', views.delete, name='delete'),
path('view/', views.view, name='view'),
配置视图信息,
from .models import valid_login as valid_login_func, delete_user, get_user
def delete(request):
if not request.session.get('user'):
return redirect('user:login')
uid = request.GET.get('uid', '')
if uid.isdigit():
delete_user(uid)
return redirect('user:index')
配置model
def dump_users(users):
fhandler = open(DATA_FILE, 'wt')
fhandler.write(json.dumps(users))
fhandler.close()
return True
def delete_user(uid):
users = get_users()
users.pop(uid, None)
dump_users(users)
return True
此时再次登录访问,删除不了自己,可以删除其他用户。
9、编辑用户
首页index.html,添加配置:<a href="{% url 'user:view' %}?uid={{ uid }}">编辑</a>
用户编辑页面view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户编辑</title>
</head>
<body>
<form action="{% url 'user:update' %}" method="POST">
{% csrf_token %}
{% if errors %}
{% for key, error in errors.items %}
{{ error }} <br/>
{% endfor %}
{% endif %}
<input type="hidden" name="id" value="{{ user.id }}">
用户名: <input type="text" name="name" value="{{ user.name }}"/> <br/>
联系方式: <input type="text" name="tel" value="{{ user.tel }}"/> <br/>
年龄: <input type="text" name="age" value="{{ user.age }}"/> <br/>
性别: <input type="radio" name="sex" value="1"
{% if user.sex != 0 %} checked="checked" {% endif %}/> 男
<input type="radio" name="sex" value="0"
{% if user.sex == 0 %} checked="checked"{% endif %} /> 女
<br/>
<input type="submit" value="修改" /> <br/>
</form>
</body>
</html>
路由url设置,
path('view/', views.view, name='view'),
path('update/', views.update, name='update'),
视图信息,
from .models import valid_login as valid_login_func, delete_user, get_user, valid_update_user, update_user
def view(request):
if not request.session.get('user'):
return redirect('user:login')
uid = request.GET.get('uid', '')
return render(request, 'user/view.html', {'user': get_user(uid)})
def update(request):
if not request.session.get('user'):
return redirect('user:login')
uid = request.POST.get('uid', '')
name = request.POST.get('name', '')
tel = request.POST.get('tel', '')
age = request.POST.get('age', '')
sex = request.POST.get('sex', 1)
is_valid, user, errors = valid_update_user(request.POST)
if is_valid:
update_user(user)
return redirect('user:index')
else:
return render(request, 'user/view.html',
{'user': user, 'errors': errors})
模型model设置,需要调整get_user,获取用户uid信息
def get_user(uid):
users = get_users()
user = users.get(uid, {}) #加入uid,为后面删除做铺垫。
user['id'] = uid
return user
def valid_update_user(params):
uid = params.get('id', '')
name = params.get('name', '')
tel = params.get('tel', '')
age = params.get('age', '')
sex = params.get('sex', '')
is_valid = True
user = {}
errors = {}
users = get_users()
user['id'] = params.get('id', '').strip()
if users.get(user['id']) is None:
errors['id'] = '用户信息不存在'
user['name'] = params.get('name', '').strip()
for uid , cuser in users.items():
if cuser['name'] == user['name'] and uid != user['id']:
errors['name'] = '用户名已存在'
is_valid = False
break
user['age'] = params.get('age', '0').strip()
if not user['age'].isdigit():
errors['age'] = '年龄格式错误'
is_valid = False
user['tel'] = params.get('tel', '0').strip()
user['sex'] = int(params.get('sex', '0').strip())
return is_valid, user, errors
def update_user(params):
uid = params.pop('id')
users = get_users()
users[uid].update(params)
return dump_users(users)
页面访问可以正常编辑和保存了。
标签:初始化,uid,get,--,request,name,user,Python3,users 来源: https://www.cnblogs.com/sunnyyangwang/p/16516400.html