Reputation: 62
in routes.py
I have a function:
@app.route('/')
@app.route('/makecsc_relocation')
def makecsc_relocation(csc_post_index):
...
makeCscRelocationRep(repname, csc_post_index)
return send_file(repname+'.xlsx', as_attachment=True, attachment_filename=repname+' '+csc_post_index+'.xlsx')
in index
, I have the following:
@app.route('/')
@app.route('/index')
def index():
...
sForm = """<form action="makecsc_relocation">
Enter postal code here: <input type="text" name="post_index" value=""><br>
<input type="submit" value="Download report" ><br>
</form>"""
If I use function that has no parameters in <form action="">
and pass to it an empty input
value, everything works fine, but as I tried to pass input post_index
as an argument to that function, I get Internal Server Error with following URL:
http://myservername/makecsc_relocation?post_index=452680
How do I fix that?
Upvotes: 0
Views: 4353
Reputation: 62
I ended up with the following solution:
POST
method to the formsForm = """<form action="makecsc_relocation" method="post">
Enter postal code here: <input type="text" name="post_index" value=""><br>
<input type="submit" value="Download report" ><br>
</form>"""
makecsc_relocation
, I added following string:csc_post_index = request.form['post_index']
and passed that to makeCscRelocationRep
Upvotes: 0
Reputation: 1125308
Function parameters are always path parameters, the <parametername>
components in the route path you register with @app.route()
. You don't have such parameters, so don't give your function any parameters. See Variable Rules in the Flask Quickstart.
Query parameters (the key=value pairs from a form, put after the ?
in the URL) end up in request.args
:
@app.route('/makecsc_relocation')
def makecsc_relocation():
csc_post_index = request.args.get('post_index') # can return None
# ...
makeCscRelocationRep(repname, csc_post_index)
return send_file(repname+'.xlsx', as_attachment=True, attachment_filename=repname+' '+csc_post_index+'.xlsx')
See The Request Object section of the Quickstart.
request.args.get(...)
if a value is optional, or if you need to convert it from a string to a different type at the same type.request.args[...]
if it is an error to not provide the value. If the query parameter is missing, a 400 Bad Request HTTP error response is given to the client.See the Werkzeug MultiDict
documentation on how this mapping works in detail.
Upvotes: 1