[Django]-'django.template.context_processors.request' issue with django-tables2 and django-admin-tools

5👍

To use the template context processors, you must pass the request object when rendering the template:

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html = template.render(context_dict, request=request)
    ...

You can simplify the code slightly by using render_to_string.

from django.template.loader import render_to_string

def render_to_pdf(template_src, context_dict=None):
    if context_dict is None:
        context_dict = {}
    html = render_to_string(template_src, context_dict, request=request)
    ...

As an aside, you shouldn’t use mutable values as defaults when defining functions. In the second example above, I have shown how you can avoid issues by using None as the default.

Leave a comment