#34427 closed New feature (fixed)

Improve error message for incorrect context processors

Reported by: Adam Johnson Owned by: David Sanders
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you write a template context processor but return the wrong type, for example None by missing the return statement:

def data(request):
    data = {"something": True}

Then currently the error message is very mysterious:

Internal Server Error: /
Traceback (most recent call last):
  ...
  File "/..../site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/..../site-packages/django/template/base.py", line 173, in render
    with context.bind_template(self):
  File "/.../lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/..../site-packages/django/template/context.py", line 254, in bind_template
    updates.update(processor(self.request))
TypeError: 'NoneType' object is not iterable

If a view returns the wrong type, Django raises a nice error message:

The view example.views.index didn't return an HttpResponse object. It returned None instead.

I suggest we do the same for context processors. If we use try/except around the updates.update() line, it will not slow down normal processing any noticeable amount, thanks to Python 3.11's “zero-cost” exception handling: https://docs.python.org/3.11/whatsnew/3.11.html#misc

Change History (8)

comment:1 by Adam Johnson, 14 months ago

Summary: Improve error message for bad context processorsImprove error message for incorrect context processors

comment:2 by Claude Paroz, 14 months ago

Triage Stage: UnreviewedAccepted

comment:3 by Shahriar Sohan, 14 months ago

Owner: changed from nobody to Shahriar Sohan
Status: newassigned

comment:4 by Shahriar Sohan, 14 months ago

Owner: Shahriar Sohan removed
Status: assignednew

comment:5 by David Sanders, 14 months ago

Owner: set to David Sanders
Status: newassigned

comment:6 by David Sanders, 14 months ago

Has patch: set

Thanks Adam, good idea 👍

I've taken a stab, left it as a TypeError but just improved the message. I'm not sure about the "It returned <blah> instead." part. With templates the "None" is hardcoded as it's a literal None check. For this I'm not sure what the best way to generalise the return value/type in English is 😁. If you have any ideas I'm open.

And as I write this the ever so quick felixx has already started reviewing though so it may all get resolved by the time you read this 🤣

comment:7 by Mariusz Felisiak, 14 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by GitHub <noreply@…>, 13 months ago

Resolution: fixed
Status: assignedclosed

In 5dba5fd:

Fixed #34427 -- Improved error message when context processor does not return a dict.

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