Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use variable in url_for in flask for parameter name

I have this code in my macro file for flask.

{% macro display_name(name, parameter, parameter_value) %}
<a href="{{ url_for('users_stats',parameter = parameter_value) }}">{{name}}</a>

{% endmacro %}

I am not able to set the value of parameter to what I pass in the function. It is coming as a plain text, How to fix this?

This is how I am calling the function.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  {{ display_name("Rahul", 'age', 15) }}

>Solution :

Try kwargs dict unpacking. I recommend research more info later.

{% macro display_name(name, parameter, parameter_value) %}
<a href="{{ url_for('users_stats', **{parameter: parameter_value}) }}">{{name}}</a>

{% endmacro %}

The point is creating dict where keys are names of parameters, values are values of corresponding parameters. Then unpack this dict via ** operator to pass these parameters and values into function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading