-1

I need to pass the feature_id value when the request is POST. 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.

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")

1 Answers1

-1

The request.args only contains data that are passed in the query string. The query string is the part of the URL after the "?". example:

example.com?q="data"

result = request.args.get("q"), result will contain "data".

So using request.args or request.form or request.get_json() all depend on your post request.

Elie Saad
  • 516
  • 4
  • 8
  • so my site passes this to the page /add_user_story?feature_id=17 do I need to convert it into a string? – Barry Marples Jul 12 '22 at 20:24
  • the query string is a string. you need to convert it to integer in the route if feature_id is expecting an integer. to pass a query string using flask url_for(), you just add it to the kwargs. – Elie Saad Jul 12 '22 at 20:29
  • yes it does need to be an integer, sorry add it to the kwargs? – Barry Marples Jul 12 '22 at 20:54