Opened 6 years ago

Closed 6 years ago

#29597 closed Bug (invalid)

AttributeError when django.template.context_processors.debug is invoked with null request

Reported by: Eric Hellman Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When the DjangoTemplates engine is used to render a template with a null request (for example, when used in render_to_string to render an email body) this line:

    if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:

raises an AttributeError: 'dict' object has no attribute 'META'
This should be replaced by:

    if settings.DEBUG and request and  request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:

otherwise, the debug context processor cannot be use for systems that render templates without a request.

Change History (2)

comment:1 by Tim Graham, 6 years ago

Has patch: unset

Can you give the code you're using? At first glance, I'd say that perhaps you've made a mistake by using render_to_string() with RequestContext instead of a plain dictionary but that could be an incorrect guess.

comment:2 by Eric Hellman, 6 years ago

Resolution: invalid
Status: newclosed

Sorry, my bug

this was caused by code that looked like this:

context = {a_dict}
render_to_string(template_name, {another_dict}, context)

because the signature of the method is

def render_to_string(template_name, context=None, request=None, using=None)

I missed that request was getting another_dict.

the code was originally targeted at the 1.5.12 signature

render_to_string(template_name, dictionary=None, context_instance=None)

but it worked in 1.8, and didn't break till 1.11.

Note: See TracTickets for help on using tickets.
Back to Top