Skip to content
Advertisement

Pass dynamic url parameter for flask url_for

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?

This is my route:

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement