简体   繁体   中英

How to get the value of a variable within an html tag in flask?

I am building an app using Flask to show nearby shops, and the user can like a shop so it can be added to their liked shops list.

My question is how can i get the value inside the <h4> tag sent to Python knowing that the name attribute is a variable. The html code is below:

<form name='likeF' method='POST' action ='{{url_for("liked")}}'>
    <h4 name="ShopName_{{loop.index}}">{{item.ShopName}}</h4>
    <p>
       Shop Description : {{item.ShopDesc}} <br>        
       <button type="submit"  class="success button like">Like</button>
       distance: {{item.ShopDistance}}
    </p>
</form>

So please how am I supposed to get back the value of {{item.ShopName}} ?

You can't send the text inside an <h4> tag (or any tag that is not a form input) back to the server, at least not without using Javascript. The easiest method would be to duplicate it in a hidden input element inside the form, such as:

<input type="hidden" name="ShopName" value="{{ item.ShopName }}">

Then you can access it using request.form["ShopName"] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM