Issue
I am trying to figure out if I can get a value from a function in my views.py in an html page.
I know I can pass values for url with the url_for, but in this case I only want a value
html:
<tbody>
{% for item in cart.items%}
<tr onclick="window.location.href='{{ url_for('views.item_detail', itemID=item) }}';">
<td>
<img src="{{ url_for('views.display_item_image', itemID=item.id ) }}" class="rounded-circle" width="100" height="100" object-fit="cover" alt="product img">
</td>
<td>{{ item.item_name }}</td>
**<td>{{ url_for('views.exampleFunction', itemID=item.id ) }}</td>**
</tr>
{% endfor %}
</tbody>
So, all the url_for are working but the one in bold I wanted just for this example to display in the tag the string I am returning in the views function bellow:
views.py
@views.route('/exampleFunction/<itemID>')
def exampleFunction(itemID):
return 'test correct'+str(itemID)
Is this even possible? as it is the url_for is displaying "/exampleFunction/1" and that wasn't my objective.
Solution
If I am understanding you correctly, for an item with ID "232", you want the line
<td>{{ url_for('views.exampleFunction', itemID=item.id ) }}</td>
to render in the html as
<td>test correct232</td>
There are few ways to accomplish this goal.
Template Global
If you want to define the function once and have it available on all of your templates automatically, you can use template_global. From the Flask documentation:
template_global(name=None)
A decorator that is used to register a custom template global function. You can specify a name for the global function, otherwise the function name will be used. Example:
@app.template_global() def double(n): return 2 * n
Register the template_global in __init__.py
:
def create_app():
app = Flask(__name__)
…
@app.template_global('exampleFunction')
def exampleFunction(itemID):
return 'test correct' + str(itemID)
…
return app
Then you can just call the function in your template like a variable:
<tbody>
{% for item in cart.items%}
<tr onclick="window.location.href='{{ url_for('views.item_detail', itemID=item) }}';">
<td>
<img src="{{ url_for('views.display_item_image', itemID=item.id ) }}" class="rounded-circle" width="100" height="100" object-fit="cover" alt="product img">
</td>
<td>{{ item.item_name }}</td>
**<td>{{ exampleFunction(item.id) }}</td>**
</tr>
{% endfor %}
</tbody>
Inline & Imported Functions
Functions can be passed to templates like any other variable.
Define Elsewhere and Import
If you want to define your function in a separate file from your route definition, it can be imported and then passed to the template.
Define Inline
A function defined in the same file as the route definition is available to pass to the template.
abc.py
def foo(input):
stringFOO = f"The input to the foo funtion was: {input}"
return stringFOO
blueprint_routes.py
from abc import foo
def bar(input):
stringBAR = f"The input to the bar function was: {input}"
return stringBAR
@blueprint.route('/example')
def example():
return render_template(
'/blueprint/example.html',
foo=foo,
bar=bar
)
Once passed to the template, the function can be called just as with the template_global
function.
example.html
<div class="centered">
In this paragraph, we use the foo function, which was imported to blueprint_routes.py. <br />
{{ foo('Foo Input') }}
</div>
<div class="centered">
In this paragraph, we use the bar function, which was defined in blueprint_routes.py. <br />
{{ bar('Bar Input') }}
</div>
Answered By - TRezendes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.