Home > Blockchain >  pass dymanic url parameter for flask url_for
pass dymanic url parameter for flask url_for

Time:04-14

I have the below URL's mapping to one form:

<a href="/home/downloads/application_one">Download Application</a>
<a href="/home/downloads/application_two">Download Application</a>
<a href="/home/downloads/application_three">Download Application</a>
<a href="/home/downloads/application_four">Download Application</a>

<form method="POST" action="{{ url_for('home.downloads') }}">

How do I pass application_one,application_two,application_three,application_four, dynamically to "url_for" in form.

below is my route:

@download.route('/downloads/<application_name>', methods=['GET', 'POST'])
def downloads(application_name):

CodePudding user response:

Pass it as second argument to url_for

{{ url_for('home.downloads', application_name = "YOUR DYNAMIC APP NAME") }}

Edit:

For the dynamic one, you simply pass the variable.

{{ url_for('home.downloads', application_name = application) }}

and when you are rendering the template, send your dynamic value.

     application = "DO WHATEVER AND ASSIGN THE VALUE HERE"
     return render_template("template.html", application = application )

Refer: https://flask.palletsprojects.com/en/2.1.x/quickstart/#url-building

  • Related