18910140161

Python-使用flask_wtf.csrf而不使用wtf_forms-堆栈溢出

顺晟科技

2022-10-19 13:47:56

184

我有一个简单的flask web应用程序,我想使用flask_wtf csrf保护,但是每当我试图运行提交表单时,我会得到一个错误,说我丢失了csrf令牌。

甚至有可能在没有wtf表单的情况下使用csrf吗? 如果是这样,我做错了什么?

我的代码:

app = Flask(__name__)
csrf = CSRFProtect(app)

@app.route("/reserve", methods=["GET", "POST"])
def reserve():
   if request.method == "GET" :
       return render_template("reserve.html", **context)


<form id="Reserve" action="/reserve" method="post">
   <!-- csrf protection -->
   <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
   <input type="text" placeholder="Name">
   <button type="submit">
        Submit
   </button>
</form>

顺晟科技:

查看您的代码,您需要用csfr初始化应用程序,下面的示例:

app = Flask(__name__)
csrf = CSRFProtect(app)

@app.route("/reserve", methods=["GET", "POST"])
def reserve():
   if request.method == "GET" :
       return render_template("reserve.html", **context)


<form id="Reserve" action="/reserve" method="post">
   <!-- csrf protection -->
   <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
   <input type="text" placeholder="Name">
   <button type="submit">
        Submit
   </button>
</form>

和HTML

app = Flask(__name__)
csrf = CSRFProtect(app)

@app.route("/reserve", methods=["GET", "POST"])
def reserve():
   if request.method == "GET" :
       return render_template("reserve.html", **context)


<form id="Reserve" action="/reserve" method="post">
   <!-- csrf protection -->
   <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
   <input type="text" placeholder="Name">
   <button type="submit">
        Submit
   </button>
</form>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航