我有一个简单的网络应用程序,\n并且我想添加 csrf 保护。但我不理解 Flask-WTF 提供的 csrf 包装器。我已经看过文档了。但仍然不明白\xe2\x80\x99 是如何工作的。
\n我的问题是:
\n(1)应用程序打包后,是否需要从路由中处理?或者烧瓶帮我处理这个?
\n(2)如果不是,我自己该如何处理?(请提供示例)。
\n注意:我不想使用 wtf 表单,我想使用自定义标签进行输入。
\n应用程序.py:
\nfrom flask import Flask, render_template\nfrom flask_wtf.csrf import CSRFProtect\n\napp = Flask(__name__)\napp.config['SECRET_KEY'] = 'secret'\ncsrf = CSRFProtect(app)\n\n@app.route('/', methods=['GET'])\ndef get_home():\n """Get home template"""\n return render_template('home.html')\n\n@app.route('/', methods=['POST'])\ndef post_home():\n """Handle posted data and do stuff"""\n return\nRun Code Online (Sandbox Code Playgroud)\nhome.html(表单):
\n<form action="#" method="post">\n <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>\n <input type="text" placeholder="Name">\n <button type="submit">\n Submit\n </button>\n</form>\nRun Code Online (Sandbox Code Playgroud)\n 我正在尝试在我的 django 应用程序中创建自定义用户模型。但是每当我尝试这样做时,我都无法使用管理面板中的“超级用户”创建的“工作人员用户”登录。它说:
“请输入员工帐户的正确用户名和密码。请注意,这两个字段都可能区分大小写。”
应用名称:后台
后端/models.py
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser,
BaseUserManager
)
# Create your models here.
class UserManager(BaseUserManager):
def create_user(
self,
username = None,
email = None,
password = None,
is_active = True,
is_staff = False,
is_admin = False
):
if not username:
raise ValueError("Users must have a username")
if not password:
raise ValueError("Users must have a password")
if not email:
raise ValueError("Users must have a email")
user_obj = self.model(
username = username …Run Code Online (Sandbox Code Playgroud) python django django-models django-admin django-authentication