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

sekizai.context_processors.sekizai seems not working #73

Closed
rocchidavide opened this issue Sep 29, 2017 · 2 comments
Closed

sekizai.context_processors.sekizai seems not working #73

rocchidavide opened this issue Sep 29, 2017 · 2 comments

Comments

@rocchidavide
Copy link

I've added sekizai context processor:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai',
                'my_cms.context_processors.site_configuration'
            ],
        },
    },
]

I have this custom tag:

@register.simple_tag(takes_context=True)
def render_page_contents(context):
    page = context['page']

    html = ''
    for pc in page.page_contents.all():
        if pc.template:
            html += render_to_string(pc.template, RequestContext({'page_content': pc}))
    return mark_safe(html)

A template rendered above:

{% load sekizai_tags %}

{% addtoblock "jsa" %}
<script type="text/javascript">alert('a');</script>
{% endaddtoblock %}

<div id="{{ page_content.css_id }}" class="{{ page_content.css_classes }}" style="border: 1px dashed forestgreen">
    <h2>{{ page_content.title }}</h2>
    <h3>{{ page_content.subtitle }}</h3>
</div>

The base.html contains:

{% load static i18n sekizai_tags %}
{% render_block "jsa" %}

I obtain this error:

You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates.

The context processor is added and I'm using RequestContext, like docs says:

For Django versions after 1.10, add sekizai.context_processors.sekizai to your TEMPLATES['OPTIONS']['context_processors'] setting and use django.template.RequestContext when rendering your templates.

I'm using Django 1.10.8 and django-sekizai 0.10.0

@ojii
Copy link
Collaborator

ojii commented Sep 29, 2017

render_to_string(pc.template, RequestContext({'page_content': pc})) the note about using RequestContext is meant for your main view. Once you start rendering a template, you need to keep the context around, even for sub-templates. You can work around it using:

from sekizai.helpers import get_varname

...

render_to_string(pc.template, Context({'page_content': pc, get_varname(): context[get_varname()]})

@ojii ojii closed this as completed Sep 29, 2017
@rocchidavide
Copy link
Author

Thank you!!

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

No branches or pull requests

2 participants