How to get the value of html attributes to use it in flask with mysql_db

I am trying to make a ecommerce site using flask and raw mysql i.e. using flask_mysqldb to write my own mysql query. I am trying to use dynamic routing to display data in a single template based on their id to avoid creating multiple templates for different products. But i am stuck as i cannot get the id of the products to pass it as a parameter in the mysql query. How to get the html tags attribute values to pass to the sql query as a parameter??
My route:

@app.route('/products/')
def product_details():
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    productID = request.args.get('id')
    print(productID)
    
    cursor.execute('SELECT productCode, productName FROM Products WHERE productCode = %s', (productID, )
    )
    products = cursor.fetchone()
    return render_template('product-details.html', products=products)

My html files

products.html:

<div>
{% for product in products %}
    <h1><a href={{url_for('product_details', pk=product.productCode) }} id={{product.productCode}}>{{product.productName}}</a></h1>
    <p>Rs. {{product.price}}</p>
    {{product.productCode}}
{% endfor %}

product-details.html:

<h1 id={{products.productCode}}>{{products.productName}}</h1>

Answer

The problem is caused by a mismatch between the query parameters you are sending to, and expecting in product_details.

You are sending pk in your url_for call but are looking for id in productID = request.args.get('id').

The solution is to look for pk in product_details: productID = request.args.get('pk').

Note that anything information outside the url_for call is not visible to Flask i.e. Flask does not have access to the <a> tag’s id attribute.