Skip to content

Context is overridden by context processors #61

@yaakovLowenstein

Description

@yaakovLowenstein

Hi,

I found an interesting issue - I don't think I'd call it a bug.

any context variable passed explicitly to a block can be overridden if a context processor provides a variable with the same name.

For example:

{% for user in object_list %}
  {% block row %}
     <tr>
       <td>{{ user.username }}</td>
    </tr>
  {% endblock row %}
{% endfor %}

In a template this will work even though you may have a user separately in the context from a context processor (django.contrib.auth.context_processors.auth) , in this block the user will be the user from the list.

However, with django-render-block because the context processors are being run after the context is set, if I add a user to the context for the block, rendering the row block would actually give you the user from the context processor not the user I added to the context.

This all takes place in the bind_template function, which is a function of RequestContext which takes in additional processors. A potential fix could be to pass in a processor (a function that takes in request and returns a dict) that returns the context passed into the block so that this manual context would override the context from the context processor. (I'm open to submitting PR if you like this)

On the other hand, maybe this is just me not realizing your not supposed to use user as a context var 🤷

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions