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")
CodePudding user response:
request.args.get is used with 'GET' method usually, then either put it in URL and function's parameter:
@app.route("/add_user_story/<int:feature_id>", methods=["GET", "POST"])
@login_required
def add_user_story(feature_id): # the function 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">
CodePudding user response:
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.