Search code examples
flaskhidden-fieldflask-wtforms

Flask: Hidden values from one page to another


I've read the other questions but still can't figure out what's wrong. I want to pass a hidden value from one page: dashboard to another: seegoodsrec Hopefully someone can help. I am using this form to obtain a hidden value:

class customerIDForm(Form):
    customerID = HiddenField()

This is then used in my dashboard:

@app.route('/dashboard/', methods=['GET', 'POST'])
@login_required
def dashboard():
    form = customerIDForm()
    customerID = form.customerID.data 
    return render_template("dashboard.html",form = form)

I feel up to here it's fine. But I can't figure out the html code. Inside a table I want the button to do the submit to my page seegoodsrec:

{% for t in TOPIC_DICT["goodsrec"] %}
<td>
<form method=post action="/seegoodsrec/">
<input type="hidden" name="customerID" value="{{j[0]}}"
    <a href="{{t[3]}}"><button type="submit" value=Save class="btn btn-primary"style=
" margin-top: 5px; margin-bottom: 5px; height:44px; margin-right: 15px">
 <span class="glyphicon glyphicon-search"></span> {{t[2]}}</button></a>
</form>
</td>
{% endfor %}

Value j[0] is a variable, depending on the row inside the table you click, the button should submit the value to page seegoodsrec. This is how seegoodsrec is set up:

@app.route('/seegoodsrec/', methods=['GET', 'POST'])
@login_required
def seegoodsrec():
    form = customerIDForm()
    customerID = request.form.get('customerID')
    return render_template("seegoodsrec.html",form = form)

The page seegoodsrec does not display the value customerID which come from j[0]. Any input is very much appreciated, thanks! I was simply trying on seegoodsrec:

<p>test{{customerID}}</p>

EDIT

Sorry, to be such a pain, but I just can't figure out the correct way to do it. Maybe a screenshots from my page helps: Screenshot from my page

So basically, what I need to do: When I click View Details I only want to see the details for customer in the row I clicked (depending on the ID). View details is going to take me to a new page called seegoodsrec. So, my idea was to pass a hidden values to page seegoodsrec which is the customer ID. This value I could then use to only display good receivers for that customer.

Thanks a lot in advance for your help.


Solution

  • If that helps anyone. Here is how I do my selection:

    <form method=post action="/seegoodsrec/">                    
    <a href="{{t[3]}}"><button type="submit" name="customerID"
    value="{{j[0]|int}}" class="btn btn-primary" style=" margin-top: 5px;
    margin-bottom: 5px; height:44px; margin-right: 15px">
    <span class="glyphicon glyphicon-search"></span> {{t[2]}}
    </button>
    </a>
    </form>
    

    In flask:

    def seegoodsrec():
    customerID = request.form.get('customerID')
    mySQL3 = SelectGoodsrec(session['ID']) #displayed goodsreceiver with user ID
    mySQL8 = SelectGoodsrecSEE(customerID)
    c, conn = connection()
    
    x = c.execute("SELECT * FROM goodsrec WHERE Gr_Cm_id = (%s)",
                          [str(customerID)])
    if int(x) < 1:
        flash("Please, create a goods receiver for this customer")
    

    mySQL8 picks up the customerID and selects the respective goods receivers.