Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#828 closed defect (worksforme)

Same function used for GET and POST

Reported by: aaronsw Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Django encourages the use of the same function for both GET and POST (not to mention DELETE) methods. For example, 'module.method' resolves to:

def method(request): ...

In the web app framework I wrote, it resolves to:

class method:
    def GET(self, request): ...
    def POST(self, request): ...

This ensures that POST functions don't accidentally leak into GETs. If both POST and GET want to reuse code, then they can add a new method to the class. This also allows this style:

(r'^/method/(.*)', 'module.method')

class method:
    GET = web.autodelegate("GET_")
    POST = web.autodelegate("POST_")
    def GET_(self, request): ... # index
    def GET_pagename(self, request): ...
    def POST_pagename(self, request): ...

which makes adding new URLs easy. The code for autodelegate is simply:

def autodelegate(prefix=''):
    def internal(self, request, arg):
        func = prefix+arg
        if hasattr(self, func): return getattr(self, func)(request)
        else: raise NotFound
    return internal

Change History (6)

comment:1 Changed 13 years ago by Jacob

FYI, there are decorators in django.views.decorators.http to control which request methods a given view allows; this is a very interesting idea, though.

comment:2 Changed 13 years ago by brantley (deadwisdom@…

This is an interesting idea. But you could still do this sort of thing, actually without changes in django. If you had your method class define call(), like so:

class MethodPage:
   def __call__(request, **kw):
      if request.GET:
          self.get(request, **kw)
      elif request.POST:
, **kw)
          self.default(request, **kw)

   def get(request, **kw):
   def post(request, **kw):
   def default(request, **kw):

And then use it in your page:

class method(MethodPage):
    def get(request, arg1, arg2):
        "Does stuff on get."
    def post(request, arg1, arg2):
        "Does stuff on post."

comment:3 Changed 13 years ago by anonymous

Oh, and finally, make it a singleton:

    method = method()

comment:4 Changed 13 years ago by hugo

Actually your code only works if you make get/post/whatever classmethods, as otherwise they will require a MethodPage object in the first parameter ;)

comment:5 Changed 13 years ago by Adrian Holovaty

Resolution: worksforme
Status: newclosed

See the comments on this ticket.

comment:6 Changed 12 years ago by Adrian Holovaty

milestone: Version 0.91

Milestone Version 0.91 deleted

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