Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26695 closed Bug (fixed)

views.generic.base.View.options() documentation doesn't match code

Reported by: Marcel Moreaux Owned by: nobody
Component: Documentation Version: 1.9
Severity: Normal Keywords: http dispatch options httpresponse
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation for views.generic.base.View.options() says:

options(request, *args, kwargs)

Handles responding to requests for the OPTIONS HTTP verb. Returns a list of the allowed HTTP method names for the view.

However,

class Some(View):
    def options(self, request, **kwargs):
        return ['GET', 'OPTIONS']

Results in

Traceback (most recent call last):
  File "/.../django/core/handlers/base.py", line 235, in get_response
    response = middleware_method(request, response)
  File "/.../django/middleware/clickjacking.py", line 31, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'list' object has no attribute 'get'

The relevant Django code is:

def dispatch(self, request, *args, **kwargs):
    if request.method.lower() in self.http_method_names:
        handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
    else:
        handler = self.http_method_not_allowed
    return handler(request, *args, **kwargs)

def options(self, request, *args, **kwargs):
    response = http.HttpResponse()
    response['Allow'] = ', '.join(self._allowed_methods())
    response['Content-Length'] = '0'
    return response

So, from the looks of it, Django expects options() to return a HttpResponse object, in line with e.g. get() and post(), in direct contradiction of the documentation.

[1] https://docs.djangoproject.com/en/1.9/ref/class-based-views/base/#django.views.generic.base.View.options

Change History (5)

comment:1 by Tim Graham, 8 years ago

If we change the sentence to "Returns a response with the list of the allowed HTTP method names for the view." does the clarify it for you?

comment:2 by Dheerendra Rathor, 8 years ago

I will suggest a bit more verbose doc "Returns a response with the list of the allowed HTTP method names in Allow header for the view"

Last edited 8 years ago by Dheerendra Rathor (previous) (diff)

comment:3 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In db2a6b6b:

Fixed #26695 -- Clarified return value of View.options().

Thanks mmoreaux and DheerendraRathor.

comment:4 by Tim Graham <timograham@…>, 8 years ago

In bad894d:

[1.10.x] Fixed #26695 -- Clarified return value of View.options().

Thanks mmoreaux and DheerendraRathor.

Backport of db2a6b6bfa9881725e70ca579a17e942a97a4df8 from master

comment:5 by Tim Graham <timograham@…>, 8 years ago

In 84698b89:

[1.9.x] Fixed #26695 -- Clarified return value of View.options().

Thanks mmoreaux and DheerendraRathor.

Backport of db2a6b6bfa9881725e70ca579a17e942a97a4df8 from master

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