cannot use current_user in jinja2 macro?


I use Flask-Login which provides the current_user object in templates. I want to write a macro to show a comment form or a log in link depending on if the user is logged in. If I use this code directly in the template, it works:

{% if current_user.is_authenticated %}
    {{ quick_form(form) }}
{% else %}
    <a href="{{ url_for('auth.login') }}">Log In with Github</a>
{% endif %}

I placed the same code in a macro and import the macro in my template.

{% macro comment_form(form) %}
    {% if current_user.is_authenticated %}
    {% endif %}
{% endmacro %}
{% from "macros/comments.html" import comment_form %}
{% extends "base.html" %}
{% block content %}
    {# ... content goes here ... #}
    {{ comment_form(form) }}
{% endblock %}

When I try to load this page, the error I get is:

jinja2.exceptions.UndefinedError: 'current_user' is undefined

Of course, the easy workaround is to pass in current_user as a parameter and use that (making the signature comment_form(user, form)), although this is a fairly ugly solution (imo).

Why doesn’t the macro use the context processor? Does it not hold the context?


The context a template is rendered in is not passed to imports unless instructed to do so. See the relevant docs.

You’re right, you don’t need to inject context as arguments to macros. You can import the macros with context and they will have access the the context of the template they’re imported in.

{% from "macros/comments.html" import comment_form with context %}

Answered By – davidism

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published