#21564 closed Bug (fixed)

Generic base view http_method_not_allowed method references self.request and should use locally scoped request

Reported by: adepue Owned by: nobody
Component: Generic views Version: 1.6
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 (last modified by timo)

The current method in django/views/generic/base.py is:

     def http_method_not_allowed(self, request, *args, **kwargs):                         
         logger.warning('Method Not Allowed (%s): %s', request.method, request.path,      
             extra={                                                                      
                 'status_code': 405,                                                      
                 'request': self.request                                                  
             }                                                                            
         )                                                                                
         return http.HttpResponseNotAllowed(self._allowed_methods())                      

The 'extra' dictionary should reference the locally scoped 'request' object instead of the class scoped member.

There is a PR open with this fix:
https://github.com/django/django/pull/2035

The side effect is that any unit test written that uses the view and manually calls the dispatch method to validate non-allowed-methods will crash with attribute not found error for request

Change History (8)

comment:1 Changed 18 months ago by adepue

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 18 months ago by claudep

  • Has patch set
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 18 months ago by adepue

Test added to PR.

comment:4 Changed 18 months ago by timo

  • Description modified (diff)
  • Needs tests unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 18 months ago by bmispelon

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

There's another instance of a similar behavior in RedirectView.get: https://github.com/django/django/blob/master/django/views/generic/base.py#L199

Would you mind fixing that one too and adding a test for it?

Thanks.

comment:6 Changed 18 months ago by timo

In addition, I was going to backport this to 1.6.X since it's a crashing issue. Could you add the following to docs/releases/1.6.1.txt as well? "Fixed a typo in :meth:django.views.generic.base.View.http_method_not_allowed that caused it to crash in some cases (#21564)." (feel free to amend as you see fit and to account for the request from bmispelon)?

comment:7 Changed 18 months ago by timo

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 18 months ago by Baptiste Mispelon <bmispelon@…>

  • Resolution set to fixed
  • Status changed from new to closed

In e2f142030b81a37e1c3187f5d336dcb6014fd1c0:

Fixed #21564 -- Use local request object when possible in generic views.

Thanks to trac user adepue for the report and original patch.

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