Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#14558 closed (fixed)

Generic View http_method bug

Reported by: pyrou Owned by: nobody
Component: Generic views Version: master
Severity: Keywords:
Cc: michael.hurni@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In generic views, you can find some http_method alias, just like in DeleteView (inherited from DeletionMixin)

post = delete 

but if you override the orginal "delete" method in your own view :

class myDeleteView(DeleteView):
    def delete(self, request, *args, **kwargs):
        print "some console log"
        return super(DeleteView, self).delete(self, request, *args, **kwargs)

your log is never printed, because your browser will use POST method, and so the original delete method is called instead of the overriden one.

Adding everytime the alias post = delete is not "DRY-ful"..

This way seems to be more "correct" for every alias:

def post(self, *args, **kwargs):
    return self.delete(*args, **kwargs)

Attachments (1)

edit.py.diff (1.4 KB) - added by pyrou 4 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 follow-up: Changed 4 years ago by lrekucki

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

This is a problem. Personally, I'm not very happy with either way of aliasing, because there is no way to get rid of the alias. With the alias always there, you can't create a view that deletes on DELETE and creates a child resource on POST (which is the original meaning of that HTTP verb).

There is also an slight inconsistency in UpdateView, which aliases POST to PUT, but IMHO this is a part of a larger issue.

comment:2 in reply to: ↑ 1 Changed 4 years ago by pyrou

  • Cc michael.hurni@… added

Replying to lrekucki:

This is a problem. Personally, I'm not very happy with either way of aliasing, because there is no way to get rid of the alias. With the alias always there, you can't create a view that deletes on DELETE and creates a child resource on POST (which is the original meaning of that HTTP verb).

There is also an slight inconsistency in UpdateView, which aliases POST to PUT, but IMHO this is a part of a larger issue.


You just have to create two methods, that will rid the "View" of its alias, it's not a problem in either way of aliasing.

class myRestUpdateView(UpdateView):
    def delete(self, request, *args, **kwargs):
        # delete
    def post(self, request, *args, **kwargs):
        # create a child ressource

The only point I want to fix is "keeping" the link between two methods when you override the original method (in my first example, the delete method)

Changed 4 years ago by pyrou

comment:3 Changed 4 years ago by russellm

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

(In [14374]) Fixed #14558 -- Modified the way PUT and DELETE HTTP methods are handled so that overridden methods will get used correctly. Thanks to pyrou for the report and patch.

comment:4 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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