0

Basically I have a for loop to generate N buttons. Each button has a name and an id to recognize the activity. When one of these buttons is clicked, a new HTML page is opened in order to display the info of that activity. I could open the page but I need to pass the value of the button or I won't know which activity was clicked. There must be a pattern to do this.

You can check the code here:

<div class="activities"> <h2>Activities</h2> {% set i = [0] %} {% for d in data %} <a href ="/activity"><button class="btn btn-info" style="margin: 10px;" value="{{ indexs[i[0]] }}">{{ d }}</button></a> {% if i.append(i.pop() + 1) %}{% endif %} {% endfor %} </div> 
@views.route('/activity') def activity(): return render_template("activity.html") 
2
  • Use Javascript to pass the buttons value when it is clicked to the backend(flask). Commented Jul 27, 2021 at 8:55
  • I tried the following but it gives me the error "; expected" <button value="{{ indexs[i[0]] }}" class="btn btn-info" style="margin: 10px;" onclick="window.location.href='{{ url_for( 'activity' , activity_id='value' ) }}';">{{ d }}</button> Commented Jul 27, 2021 at 9:04

1 Answer 1

1

need to pass the value of the button

Simplest way I can think of is using URL query parameters, simplified example:

HTML snippet:

<div> <a href="/activity?buttonid=first"><button>first</button></a> <a href="/activity?buttonid=second"><button>second</button></a> <a href="/activity?buttonid=thired"><button>third</button></a> </div> 

Flask:

from flask import request ... @views.route('/activity') def activity(): return "You clicked " + request.args["buttonid"] + "button" 

must be a pattern to do this

FORMs (HTML tag <form>) are currently in use for this purpose.

Sign up to request clarification or add additional context in comments.

5 Comments

I can't do that, it must be a loop.
This is simplified example, you might use your loop, for example if you wish to have text on button as buttonid, just change in your template <a href ="/activity"> to <a href ="/activity?buttonid={{d}}">
<button value="{{ indexs[i[0]] }}" class="btn btn-info" style="margin: 10px;" onclick="window.location.href='{{ url_for( 'activity' , activity_id='value' ) }}';">{{ d }}</button> I tried this with no success. Then I changed the route for route('/activity/<int:activity_id>') def activity(activity_id)
If you did find own solution to stated question, you might post it as answer (self-answer)
No, it does not work... I get an error "; expected" in the html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.