0

I have two def for creating question and previewing question.

I want to pass a question's ID from def create() to def preview(). But the def preview() cannot realize the ID.

I have try using return redirect(url_for('preview', question_id=question_id)) and in my def preview() I used question_id = request.args.get('question_id',type=str).

How can I fix that?

My code is here:

@app.route("/create/", methods=['GET','POST'])
def create():
    question = "How are you?"
    question_id = "123456"
    return redirect(url_for('preview'), question_id=question_id)

@app.route("/preview/", methods=['GET','POST'])
def preview():
    question_id = request.args.get('question_id', type=str)
    print(question_id)
3
  • Can you share the error message ? (if there's one) Commented Dec 17, 2019 at 13:32
  • 1
    By the way, you're passing question_id param to the url_for function instead of redirect. Please correct to : redirect(url_for('preview'), question_id=question_id) Commented Dec 17, 2019 at 13:35
  • It should show me the ID on the page but it just show me a blank page! Commented Dec 17, 2019 at 13:36

1 Answer 1

1

In the preview route, you're doing print(question_id) which won't write or render anything to a page, it will only print the value to your console (stdout), that's all.

Try with render_template_string like so :

from flask import render_template_string

[...]



@app.route("/preview/", methods=['GET','POST'])
def preview():
    question_id = request.args.get('question_id', type=str)
    return render_template_string('question_id = {{ question_id }}', question_id=question_id)

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.