Opened 6 years ago

Closed 6 years ago

Last modified 5 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 6 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 6 years ago by Łukasz Rekucki

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

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 6 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 6 years ago by pyrou

Attachment: edit.py.diff added

comment:3 Changed 6 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(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 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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