Use Flask to set a Boolean parameter according to whether of two buttons has been pressed

Hi,
I’m a beginner with Flask, and I have difficulty managing its relationship with HTML. I’m dealing with the improvement of existing code.
I have an HTML page containing two buttons that tentatively are coded like this:

<a href="{{ url_for('report', rowData=test_id) }}"><button class="btn"><i class="fa fa-download"></i> Download 1</button></a>
<a href="{{ url_for('report', rowData=test_id) }}"><button class="btn"><i class="fa fa-download"></i> Download 2</button></a>

This is related to two Python files: home.py contains the main Flask entry point

@app.route("/", methods=['GET', 'POST'])
def index():


and then the rows

@app.route("/report/report/<rowData>")
def report(rowData):
	rep_file_name = rep.generate(rowData)
	return send_file(rep_file_name, as_attachment=True)

while report.py implements the generate function, def generate(test_id).

I am struggling to differentiate the two buttons in order to send to report.py an additional Boolean parameter: if I press the Download 1 button the variable should be set to False, while if I press the Download 2 button the variable should be set to True. I’ve made several attempts but I keep getting errors. Could you please help me?
The code is much more complicated thant that; I hope I have provided the bare minimum of information sufficient to understand the issue, but if not, I am ready to provide further details.
I look forward to hearing from you.
Thanks in advance,
Alessia

Hmm, you have buttons inside links. That may be a bit problematic, especially if this ends up inside a form. I would recommend either using buttons or using links (<a href>, not the <link> tags that go in the head).

But the easiest way to add extra information is to add another parameter. Just like you set rowData, you can set something else. Any parameters that aren’t handled by placeholders will become query parameters; they then show up in request.args as you’re processing the request. Be aware that you can only send strings this way, not booleans; one effective way to do this is to have the parameter exist on one of the links and not on the other, and then simply probe for the presence of that parameter in request.args.

1 Like

HI,
Thank you so much for your reply; your suggestion “Be aware that you can only send strings this way, not booleans” was ultimately decisive.
At the beginning of the definition of the report function I added the line
button_pressed = request.args.get('button_pressed', type = str)
and after that I inserted an if statement that checked the value of the button_pressed string which is associated to each button in the HTML code; based on this it assigned the value to the Boolean variable to be sent to the generate function. At first this seems to me the simplest and cleanest way to to get the desired result, but from this starting point I plan to deepen my Python knowledge in order to optimize my code.
Thanks again, and best regards!
Alessia