1

I have the following options within my HTML template:

  ```<form action="/submit" method="POST">
    <div class="form-group">
      <h3>Where to scrape from</h3>
      <select name="articles">
        <option value="">Select Site</option>
        <option value="Guardian">GUARDIAN</option>
        <option value="BBC">BBC</option>

      </select>
    </div>
    <input type="submit" value="Submit" class="btn" />
  </form>```

and at the moment, I can't figure out how to include an if statement to link different options to different templates.

@app.route('/submit', methods =['POST'])
def submit():
    if request.method == 'POST':
        return render_template('scraped.html', s1=summary, s2=summary2)

Here, I would like to create a statement like:

if option ['BBC']
        return render_template('X.html')
        else return render_template('Y.html')

What is the correct syntax to do that? Thanks!

2
  • Forgive me if I did not understand your question correctly. But are you asking how to switch routes depending on form input? i.e. if user select "Guardian" then route to (X.html) and if use select "BBC" then route to ("Y.html") Commented May 25, 2020 at 15:19
  • @AkibRhast Yes that's exactly what I wonder about. I just recently got to Flask and Python in general. Thanks! Commented May 25, 2020 at 16:13

2 Answers 2

1
def submit():
    if request.method == 'POST':
        select = request.form.get('articles')
        if select == "Gaurdian":
            return redirect(url_for("X_route"))
        if select == "BBC":
            return redirect(url_for("Y_route))
    return render_template('scraped.html', s1=summary, s2=summary2)

@app.route("/X_route")
def X_route():
    return render_template("X.html")

@app.route("/Y_route")
def Y_route():
    return render_template("Y.html")

Hope that helps :D

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

1 Comment

Perfect! Works as intended. Thanks!!
0

As I understand, you should use WTForms and get access to option via Form object.

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.