0

I have webpage which uses flask/python framework, and also a bootstrap button is present in webpage. Now I want to pass some values or parameters when a button/link is clicked, how can it be done?

In the below code Generatebill is the button that I need to pass some parameters when clicked.

<div class="card text-white bg-info mb-3" style="max-width: 18rem;">
<div class="card-body">
<h5 class="card-title">Washinton laundries</h5>
<H5><p class="card-text">5 Kg </p></H5>
    <H5><p class="card-text">Rate : 200 Rs</p></H5>
    <a href="generatebill" class="btn btn-primary">Generatebill</a>
</div>
</div>
MackM
  • 2,906
  • 5
  • 31
  • 45

1 Answers1

0

To pass parameters through the view of your webapplication:

Define the parameters in your app's code :

@app.route("/path/<parameter>")
def view(parameter):
    ...

(To avoid confusions: the parameter needs to be put in between <> brackets in the url)

Use url_for() to reference the view with the parameters:

<a href = "{{url_for('view'), parameter = 'parameter_content'}}"></a>
MisterMM23
  • 220
  • 5
  • 14
  • i tried the above code but when writing this func and parameter my code errors out with invalid syntax. @app.route("/path/5kg") def view(5kg): @MisterMM23 – sanjeev chaudhary Jul 13 '18 at 15:38
  • Don‘t forget to put your variable into `<>` brackets. My bad, I forgot to precise this. Where exactly does he show the SyntaxError in the code? – MisterMM23 Jul 13 '18 at 15:43
  • i tried both def view(5kg): and def view(<5kg>): and here only python script error out with invalid syntax error?? @MisterMM23 – sanjeev chaudhary Jul 13 '18 at 15:55
  • You should put the `<>` brackets in the url so: @app.route(“/path/<5kg>”) – MisterMM23 Jul 13 '18 at 15:58
  • i did that as well didnt worked out for me @MisterMM23 – sanjeev chaudhary Jul 13 '18 at 16:01
  • the button shuld return to new webpage and with the parameters as well so i am returning below line in func, is that correct?? return render_template("generatebill.html") @MisterMM23 – sanjeev chaudhary Jul 13 '18 at 16:06
  • Do you want to treat the parameters and then redirect to a new page, differen from what you have shown in your question? Then you schould `return redirect(url_for(“name_of_the_view_of_the_new_page”))` – MisterMM23 Jul 14 '18 at 12:24