0

I am new to Flask and I am just trying to pass 4 arguments that I got from my form to another python function. When I submit my form, I am getting this error : "TypeError: search() missing 4 required positional arguments: 'keywords', 'starting_date', 'ending_date', and 'country'"

I verified and all of my variables have data from the form, so they are not empty

`

@app.route('/')
@app.route('/index', methods=["GET", "POST"])
def index():
    if request.method == "POST":
        keywords = request.form.get('keywords')
        starting_date = request.form.get('starting_date')
        ending_date = request.form.get('ending_date')
        country = request.form.get('country')
        return redirect(url_for("search", keywords=keywords, starting_date=starting_date, ending_date=ending_date, country=country))      
    else:
        return render_template("index.html")

@app.route('/search', methods=["GET"])
def search(keywords, starting_date, ending_date, country ):
    return render_template('result.html', title='Result')
Pierre56
  • 537
  • 8
  • 23

2 Answers2

2

You'll need to define your search route as

@app.route('/search/<keywords>/<starting_date>/<ending_date>/<country>', methods=["GET"])

to get your current implementation to work, see Variable Rules section in Flask's doc.

This is, however, problematic. For one, you probably don't want such a messy URL.

A better approach is to ask Flask to send your data to search as query parameters and retrieve it through request.args. Redefine search as

@app.route('/search', methods=["GET"])
def search():
    keywords = request.args['keywords']
    starting_date = request.args['starting_date']
    ending_date = request.args['ending_date']
    country = request.args['country']

    # perform search

    return render_template('result.html', title='Result')

Now url_for will generate a URL of the form /index?keywords=<value>&starting_date=<value>....

See this great SO answer for all the ways to get data from a request: https://stackoverflow.com/a/16664376/1661274

Simeon Nedkov
  • 1,097
  • 7
  • 11
1

You need to render the template with your form parameters.

Try this:

@app.route('/index', methods=["GET", "POST"])
def index():
    if request.method == "POST":
        keywords = request.form.get('keywords')
        starting_date = request.form.get('starting_date')
        ending_date = request.form.get('ending_date')
        country = request.form.get('country')
        return render_template('result.html', keywords=keywords, starting_date=starting_date, ending_date=ending_date, country=country)
    else:
        return render_template("index.html")
luuu
  • 40
  • 8