Home > Back-end >  i have to use the argument passed in flask route inside the html pages.. how to do it?
i have to use the argument passed in flask route inside the html pages.. how to do it?

Time:09-07

Python file:

@app.route('/home/<argument>')
def home_page(argument):
    return render_template('home.html', keys=argument)

HTML file:

<!-- inside the url http://127.0.0.1:5000/home/age -->
{{ if argument == "age" }}
    <p>Age 50</p>
{{ else }}
    <p>Invalid</p>
{{ endif }}

This results in the following error:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'argument'

CodePudding user response:

I think syntax for if else in jinja2 is wrong it should be like this:

Code:

{{% if argument == "age" %}
   <p>Age 50</p>
{% else %}
   <p>Invalid</p>
{% endif %}}

CodePudding user response:

First, modify your view function, remember to put a slash after the dynamic URL variable /home/<argument>/

@app.route('/home/<argument>/')
def hello_world(argument):
    return render_template('home.html', keys=argument)

And, there in the view function, you have passed the argument data in the keys variable. So, call the keys in your home.html file with the correct syntax.

{% if keys == "age" %}
    <p>Age 50</p>
{% else %}
    <p>Invalid</p>
{% endif %}

This will solve your problem. Happy coding

  • Related