You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running Flask normally, everything described below works absolutely fine.
When running a build in Frozen-Flask however, I run into the issue where my dynamically built route string won't allow me to concatenate a variable (containing a string) with a string for use in a url_for() function within Jinja2. But only when the variable data is originating from Python such as render_template().
# The following does NOT work
{%macromy_macro(foo) %}
<ahref="{{ url_for('index_' ~ foo) }}">Link text</a>
{%endmacro%}
Although the above runs fine in Flask, when I do a build with Frozen-Flask I get the following error:
werkzeug.routing.BuildError: Could not build url for endpoint 'index_'. Did you mean...
As you can see, the value of foo (or the string value bar) is missing. It should have been index_bar.
So I tried this instead:
# This also does NOT work
{%macromy_macro(foo) %}{%setroute = 'index_' ~ foo%}
<ahref="{{ url_for(route) }}">Link text</a>
{%endmacro%}
The above produces the exact same error.
So I tried this to try to better understand the problem:
# This works correctly
{%macromy_macro(foo) %}{%setroute = 'index_' ~ 'bar'%}
<ahref="{{ url_for(route) }}">Link text</a>
{%endmacro%}
So I further tried this:
# This also works correctly
{%macromy_macro(foo) %}{%setfoo2 = 'bar'%}{%setroute = 'index_' ~ foo2 %}
<ahref="{{ url_for(route) }}">Link text</a>
{%endmacro%}
So basically, I can create a dynamic route string for use by url_for() but only when the variable data doesn't come from the Flask routes file (such as my routes.py), only if the variable is created within Jinja2.
The text was updated successfully, but these errors were encountered:
Your routes.py file in the example only defines some_route. I'm trying to understand your setup here, do you actually have multiple routes with endpoints index_bar, index_other etc? If so you may want to create a true dynamic route as follows:
then in your templates you can access these via url_for as url_for(somewhere, foo='bar') and skip out the macro and string concatenation in the template.
But this isn't the problem that I am trying to solve.
The issue (ignoring jinja macros for this explanation) is that url_for will not accept a concatenate a string with a variable originating in Flask when running a Build, but it works fine when running Flask normally.
So, for example if the value coming from Flask is language = 'en' like this:
When running Flask normally, everything described below works absolutely fine.
When running a build in Frozen-Flask however, I run into the issue where my dynamically built route string won't allow me to concatenate a variable (containing a string) with a string for use in a
url_for()
function within Jinja2. But only when the variable data is originating from Python such asrender_template()
.routes.py
base.html
includes.html
Although the above runs fine in Flask, when I do a build with Frozen-Flask I get the following error:
As you can see, the value of foo (or the string value
bar
) is missing. It should have beenindex_bar
.So I tried this instead:
The above produces the exact same error.
So I tried this to try to better understand the problem:
So I further tried this:
So basically, I can create a dynamic route string for use by
url_for()
but only when the variable data doesn't come from the Flask routes file (such as myroutes.py
), only if the variable is created within Jinja2.The text was updated successfully, but these errors were encountered: