def init(): """ 初始化数据库 :return: """ password = Public.md5("admin", "123456") if not models.UserInfo.objects.filter(username="******").first(): models.UserInfo.objects.create(username="******", password=password)
def login(request): """ 用户登录 :param request: :return: get:登录页面 post:登录认证 """ code = Public.code(4) if request.method == "POST": user = request.POST.get("username") pwd = request.POST.get("password") pwd = Public.md5(user, pwd) path = request.GET.get("path") if models.UserInfo.objects.filter(username=user, password=pwd).first(): request.session["name"] = user if path: return redirect(path) else: return redirect("/platform/") else: if path: return redirect(request.GET.get("path")) else: return render(request, "login.html", {"msg": "用户名或密码错误!", "code": code}) return render(request, "login.html", {"code": code})
def add_user(request): """ 创建用户 :param request: :return: platform页面 """ user = request.session.get("name") if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") if not username.strip(): return render(request, "user/add_user.html", {"user_msg": "用户名不能为空!", "user": user}) if not password.strip(): return render(request, "user/add_user.html", {"pwd_msg": "密码不能为空!", "user": user}) password = Public.md5(username, password) if not models.UserInfo.objects.filter(username=username).first(): models.UserInfo.objects.create(username=username, password=password) else: return render(request, "user/add_user.html", {"msg": "用户名已经存在!", "user": user}) return redirect("/user_list/") return render(request, "user/add_user.html", {"user": user})
def edit_user(request): """ 编辑用户 :param request: :return: """ user = request.session.get("name") if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") user_id = request.POST.get("user_id") obj = models.UserInfo.objects.get(id=user_id) if not password.strip(): return render(request, "user/edit_user.html", {"username": obj.username, "user_id": obj.id, "pwd_msg": "密码不能为空!", "user": user}) password = Public.md5(username, password) obj = models.UserInfo.objects.filter(username=username).first() obj.password = password obj.save() return redirect("/user_list/") user_id = request.GET.get("id") obj = models.UserInfo.objects.get(id=user_id) return render(request, "user/edit_user.html", {"username": obj.username, "user_id": obj.id, "user": user})
#!/usr/bin/env python3 import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HostManagement.settings") import django django.setup() from user import models from user.public import Public # 批量导入用户 data = [ models.UserInfo(username="******".format(i), password=Public.md5("user{}".format(i), "123")) for i in range(100) ] models.UserInfo.objects.bulk_create(data)