-1

I want to pass the url as parameter in _header.html, so that in future in there is any change arrive then we can change in only through one function, this _header.html is included in base.html, I am new in flask so for I just use {% urlparameter% I don't know this right or not. Please tell me how to do that, is we required to write any python function for this?

Here I am using http://192.168.206.133:5001/metering In Future this will change, so any good approach to handle such type of case

_header.html

<h1 class="brand"><a href="http://192.168.206.133:5001/metering"">{% url parameter %}</a></h1>

base.html

<body ng-app="flaskhorizonapp">
    {% block content %}
        <div id="container">
            <div class='topbar'>
                {% include "_header.html" %}
            </div>
        </div>
    {% endblock %}
</body>
geeks
  • 2,025
  • 6
  • 31
  • 49

1 Answers1

1

In templates, variable references are encapsulated in {{ and }}, not {% and %}. So you should change the contents of _header.html to:

<h1 class="brand"><a href="{{ urlparameter }}">{{ urlparameter }}</a></h1>

Now the value of the context variable urlparameter can be rendered by the template. Here is a simple Flask app to do that:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('base.html', urlparameter='http://www.stackoverflow.com')

app.debug = True
app.run()

So you need to pass the variable urlparameter to render_template(). Note that I enabled debugging as this will help you find errors such as this during development.

render_template() assumes that your templates are located in a subdirectory of the directory containing your flask script. The subdirectory must be named templates.

Take a look here for more details.

user47
  • 1,080
  • 11
  • 28
mhawke
  • 84,695
  • 9
  • 117
  • 138