def user_register(request): if request.method == "GET": return render(request, 'user/user_register.html') elif request.method == "POST": try: username = request.POST.get("username") password = request.POST.get("password") email = request.POST.get("email") icon = request.FILES.get("icon") print(username) print(password) print(email) print(icon) user = UserModel() user.username = username # 添加数据安全 password = generate_password(password) print(password) user.password = password user.email = email user.icon = icon user.save() request.session["user_id"] = user.id return redirect(reverse("axf:mine")) except Exception as e: return redirect(reverse("axf:user_register")) else: raise Exception("不被支持的请求")
def post(self): username = request.form.get("username") password = request.form.get("password") email = request.form.get("email") icon = request.form.get("icon") data = {"msg": "ok", "status": "422"} if not username or not password: data["msg"] = "參數不正確" return jsonify(data), 422 user = UserModel() user.username = username user.password = generate_password(password) user.email = email user.icon = icon try: db.session.add(user) db.session.commit() data["status"] = "201" except Exception as e: data["status"] = "901" data["msg"] = str(e) return jsonify(data), 201