Search code examples
djangodjango-templatesdjango-sekizai

Sekizai TemplateSyntaxError where no sekizai is used


First of all this is Django 1.11.2 and sekizai 0.1 and this is in my settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS':[],
        'APP_DIRS': True,
        'OPTIONS': {
            'builtins': ['mypage.templatetags.everywhere'],
            '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',
                'django.template.context_processors.i18n',

            ],
        },
    },
]

Sekizai context processor included as it should be, however I still get this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/ubuntu/workspace/project/apps/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/wsgi.py", line 157, in __call__
    response = self.get_response(request)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 124, in get_respon e
    response = self._middleware_chain(request)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 43, in inner
    response = response_for_exception(request, exc)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 93, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ubuntu/workspace/hys/views/acc_settings.py", line 116, in horoscopedata
    context)
  File "/home/ubuntu/workspace/project/apps/django/shortcuts.py", line 21, in render_to_response
    content = loader.render_to_string(template_name, context, using=using)
  File "/home/ubuntu/workspace/project/apps/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/home/ubuntu/workspace/project/apps/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 207, in render
    return self._render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/home/ubuntu/workspace/project/apps/classytags/core.py", line 153, in render
    return self.render_tag(context, **kwargs)
  File "/home/ubuntu/workspace/project/apps/sekizai/templatetags/sekizai_tags.py", line 91, in render_tag
    if not validate_context(context):
  File "/home/ubuntu/workspace/project/apps/sekizai/templatetags/sekizai_tags.py", line 44, in validate_context
    "You must enable the 'sekizai.context_processors.sekizai' template "
TemplateSyntaxError: You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates.

What is more irritating is that the specific view, template and child template doesn't use nor load sekizai for its rendering. The only place where sekizai in this rendering chain is used is at the very top, in the main parent, that this specific template extends.

Any ideas what this is? I have no clue how to solve this. There is no obvious reason why this location is affected, simmilar aren't and location that actually use sekizai aren't as well.

EDIT as requested the view:

def vvv(request):
    template = "settings/template.html"
    context = RequestContext(request)

    if request.user.is_authenticated():
        usr_status = get_user_status(request.user)
        context['usr_status'] = usr_status
        if model.objects.filter(user_id=request.user.id).exists():
            if usr_status ==VIP:
                data = model.objects.filter(user_id=request.user.id).order_by('-main')
                remaining = MAX_MAIN_VIP-model.objects.filter(user_id=request.user.id).count()
            else:
                data = model.objects.filter(user_id=request.user.id,main=True)
                remaining = MAX_MAIN_AUTH-model.objects.filter(user_id=request.user.id).filter(main=True).count()
        else:
            data = None
            remaining = None

        return render_to_response(template,
            {'form':Form, 'data':data,'remaining':remaining},
            context)

Solution

  • You could use render() shortcut function,

    https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render

    render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
    

    render() is a brand spanking new shortcut for render_to_response in 1.3 that will automatically useRequestContext that I will most definitely be using from now on.

    Change the last line,

    return render(request, template, {'form':Form, 'data':data,'remaining':remaining, 'usr_status':usr_status})
    

    Remove the lines,

    context = RequestContext(request)
    
    context['usr_status'] = usr_status