Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's wrong with this Jinja template when using {% elif %} #1253

Closed
haimat opened this issue Jul 1, 2020 · 4 comments · Fixed by #1665
Closed

What's wrong with this Jinja template when using {% elif %} #1253

haimat opened this issue Jul 1, 2020 · 4 comments · Fixed by #1665
Assignees
Milestone

Comments

@haimat
Copy link

haimat commented Jul 1, 2020

I have the following Jinja template:

{% if new_data_set_created is defined %}
    {% if new_data_set_created == True %}
        The new image data set <strong>{{ form_data["data_set_name"] }}</strong> has been created.
        {% set form_data = {"data_set_name": "", "client": "", "project": ""} %}
    {% else %}
        The new image data set <strong>{{ form_data["data_set_name"] }}</strong> could not be created.<br />
    {% endif %}
{% elif deleted_data_set is defined %}
    The image data set <strong>{{ deleted_data_set }}</strong> has been deleted from DB and from disk.
    {% set form_data = {"data_set_name": "", "client": "", "project": ""} %}
{% else %}
    {% set form_data = {"data_set_name": "", "client": "", "project": ""} %}
{% endif %}

However, when rendering this template via Flask I get the error message that form_data in line 3 of this code section is undefined (which is not the case, I double-checked that):

jinja2.exceptions.UndefinedError: 'form_data' is undefined

Now when I remove the elif block from that code section above (i.e. lines 8-10) then the very same template does work again and line 3 does not lead to an error any more.

I am calling this template like so:

return render_template(
    "image-data-sets.html",
    new_data_set_created=True,
    form_data=request.form,
    data_sets=ImageDataSet.query.all(),
    clients=clients,
)

I double-checked that request.form (and thus form_data within the template) is not None. And as said, I don't make any changes to this call, I just remove those lines from the template and then it's working.

So my question: Why does removing the elif block fix this issue, and why is this error raised in the first place? What's wrong with my if-elif-else-endif block? Is this a bug or am I missing something?

@ovezovs
Copy link

ovezovs commented Jul 2, 2020

I was able to reproduce the issue as it is described. I did some research on this, however, wasn't able to find the cause.

@davidism
Copy link
Member

davidism commented Jul 2, 2020

The use of set here is unnecessary if not incorrect, but I'm not sure if that's causing the issue. Would need to dump the compiled code to see what's being generated and see if that reveals anything. Otherwise step through the frames to see what's defined at that point in the execution.

@ovezovs
Copy link

ovezovs commented Jul 2, 2020

The use of set here is unnecessary if not incorrect, but I'm not sure if that's causing the issue. Would need to dump the compiled code to see what's being generated and see if that reveals anything. Otherwise step through the frames to see what's defined at that point in the execution.

set is, indeed, causing the issue I think. When I commented out the first set on line 4, everything works fine. However, when I executed the elif block and printed out data_form["data_set_name"], the set within elif didn't cause any issues.

UPDATE: Just traced through the code, trying to update the data_form dict is what is causing the issue on line 4. When set command is executed, this is what I got in my debugger: form_data: missing. If I am not mistaken, that's why it is giving the Undefined error.

@davidism
Copy link
Member

davidism commented Jul 2, 2020

An initial guess it that setting a name that's passed in to the render context is not intended to be supported, and there's something going on with that in the runtime. So we need to identify the code that's handling that and then decide whether to change it or raise a better error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants