Laura Galera
Laura Galera

Reputation: 119

Pass value to new page in Flask when button is clicked

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")

Upvotes: 0

Views: 737

Answers (1)

Daweo
Daweo

Reputation: 36550

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.

Upvotes: 1

Related Questions