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
Fix AttributeError in base.html when called without response_headers
#5981
Fix AttributeError in base.html when called without response_headers
#5981
Conversation
Backport of my fix for encode#5981 This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Alternatives: * This could alternatively be solved in the `items` filter, making it just check for null before continuing. * If there was an easy way to delegate `handler404` etc to a DRF-based implementation which added the correct context, that might be an ideal solution. I couldn't find an easy way to do this.
Backport of my fix for encode#5981 This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Alternatives: * This could alternatively be solved in the `items` filter, making it just check for null before continuing. * If there was an easy way to delegate `handler404` etc to a DRF-based implementation which added the correct context, that might be an ideal solution. I couldn't find an easy way to do this.
This seems like a preferable solution to me. |
This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Originally I solved this in the template by guarding the `for` loop with a `if response_headers`. However @rpkilby expressed a preference for doing the check inside the `items` filter instead.
38ab10d
to
9012662
Compare
👍 done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Nice. Thanks.
Backport of my fix for encode#5981 This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Alternatives: * This could alternatively be solved in the `items` filter, making it just check for null before continuing. * If there was an easy way to delegate `handler404` etc to a DRF-based implementation which added the correct context, that might be an ideal solution. I couldn't find an easy way to do this.
Backport of my fix for encode#5981 This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Alternatives: * This could alternatively be solved in the `items` filter, making it just check for null before continuing. * If there was an easy way to delegate `handler404` etc to a DRF-based implementation which added the correct context, that might be an ideal solution. I couldn't find an easy way to do this.
Cherry-picked from 9629886 in 3.9.1.
Cherry-picked from 9629886 in 3.9.1.
Description
We use a
404.html
which extendsrest_framework/base.html
. With the defaulthandler404
implementation, this template is rendered when you encounter aResolver404
. With DRF <3.6 this worked fine. Since then it throws anAttributeError
. I traced the problem to 0173e9b.Since
Resolver404
goes via django'shandler404
and not via DRF's exception handling,it doesn't get any extra context.
Since the
|items
filter doesn't check forresponse_headers
being null (missing) it attemptsto call
<None>.items()
, thus raising an AttributeError.This fixes the problem by checking for the presence of
response_headers
before calling|items
with it.Possible alternative solutions
items
filter, making it just check for null before continuing. That might mask other problems elsewhere though.handler404
etc to a DRF-based implementation which added the correct context, that might be more ideal. I couldn't find an easy way to do this.