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

def X_route():
    return render_template("X.html")

def Y_route():
    return render_template("Y.html")

Hope that helps 😀

Solution 2 :

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

Problem :

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>

    <input type="submit" value="Submit" class="btn" />

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!


Comment posted by Akib Rhast

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

Comment posted by GOONhoon

@AkibRhast Yes that’s exactly what I wonder about. I just recently got to Flask and Python in general. Thanks!

Comment posted by GOONhoon

Perfect! Works as intended. Thanks!!