0

I am working on a flask project.

I have created a from with following description

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired

class SearchRestaurantForm(FlaskForm):
    query = StringField('Query', validators=[DataRequired()])
    searchButton = SubmitField('Search')

This form is accessed in a route function named search as shown below.

@app.route("/search", methods=['GET', 'POST'])
def search():
    form = SearchRestaurantForm()
    if form.validate_on_submit():
        query_ = form.query.data
        return redirect('/result?query='+query_)
    return render_template('search.html', title='Search', form = form)

I want to use the data of the StringField query in another route function named result as shown below.

@app.route("/result", methods=['GET'])
def result():
    query__ = query_
    restaurants = find_appro_res(query__)
    return render_template('result.html', res_list=restaurants)

I don't know how can I access the str of search function in result function.

I am new in flask.

Madhuri Patel
  • 1,270
  • 12
  • 24
Stolaine
  • 25
  • 4

1 Answers1

0

If you want the request parameter to use in endpoints in flask, you can always use flask's request To access incoming request data, you can use the global request object of flask.

For solving your problem modify the result function as below:

from flask import request
@app.route("/result", methods=['GET'])
def result():
   query__ = request.args.get('query') # This will capture the request argument
   restaurants = find_appro_res(query__)
   return render_template('result.html', res_list=restaurants)
ShivaGaire
  • 2,283
  • 1
  • 20
  • 31