简体   繁体   English

Flask 将值传递给 POST 方法

[英]Flask passing value into POST method

I need to pass the feature_id value when the request is POST.当请求是 POST 时,我需要传递 feature_id 值。 but I get a null value from the following code.但我从下面的代码中得到一个空值。

@app.route("/add_user_story", methods=["GET", "POST"])
@login_required
def add_user_story():
    feature_id=request.args.get("feature_id")
    print(feature_id)
    if request.method == "POST":
        
        user_story = User_stories(
            user_id=session["user"],
            feature_id =feature_id,
            as_a=request.form.get("as_a"),
            i_want=request.form.get("i_want"),
            so_that=request.form.get("so_that"),
            given=request.form.get("given"),
            when=request.form.get("when"),
            then=request.form.get("then")
        )
        db.session.add(user_story)
        db.session.commit()
        
    return render_template("logged_in/add_user_story.html") 

I have checked the terminal and my print statement has the value so I know it getting that far, To test if the form works I have replaced feature_id =feature_id with feature_id =24, and it successfully posted the data.我检查了终端,并且我的打印语句具有值,所以我知道它已经走了那么远,为了测试表单是否有效,我已将 feature_id =feature_id 替换为 feature_id =24,并且它成功发布了数据。

I have also tried我也试过

# LOGGED IN - Add User Story
@app.route("/add_user_story", methods=["GET", "POST"])
@login_required
def add_user_story():
    if request.method == "POST":
        
        user_story = User_stories(
            user_id=session["user"],
            feature_id=request.args.get("feature_id"),
            as_a=request.form.get("as_a"),
            i_want=request.form.get("i_want"),
            so_that=request.form.get("so_that"),
            given=request.form.get("given"),
            when=request.form.get("when"),
            then=request.form.get("then")
        )
        db.session.add(user_story)
        db.session.commit()
        
    return render_template("logged_in/add_user_story.html")

request.args.get is used with 'GET' method usually, then either put it in URL and function's parameter: request.args.get 通常与 'GET' 方法一起使用,然后将其放入 URL 和函数的参数中:

@app.route("/add_user_story/<int:feature_id>", methods=["GET", "POST"])
@login_required
def add_user_story(feature_id): # functions gets a parameter
    feature_id=request.args.get("feature_id")
    print(feature_id)
    if request.method == "POST":

or (the best way for post) add new input with defined value in your form:或(发布的最佳方式)在表单中添加具有定义值的新输入:

<input type="hidden" name="feature_id" id="feature_id" value="24">

The request.args only contains data that are passed in the query string. request.args 仅包含在查询字符串中传递的数据。 The query string is the part of the URL after the "?".查询字符串是 URL 中“?”之后的部分。 example:例子:

example.com?q="data" example.com?q="数据"

result = request.args.get("q"), result will contain "data". result = request.args.get("q"),结果将包含“数据”。

So using request.args or request.form or request.get_json() all depend on your post request.因此,使用 request.args 或 request.form 或 request.get_json() 都取决于您的发布请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM