GET AND POST METHOD

1,279 views
Skip to first unread message

Nightfury

unread,
Jul 31, 2012, 4:46:20 AM7/31/12
to cherryp...@googlegroups.com
i have the following code and i want to rebult it using GET method in if condition.how can i do this.

class client:
    def index(self,name=None):
        if name :
            ClientObj = Collection()
            print "name in view",name
           
            ClientObj.name=name
            loginBll.createClient(ClientObj)
       
        return open(os.path.join(MEDIA_DIR, u"clients.html"))
    index.exposed = True

Daniel Bryan

unread,
Jul 31, 2012, 4:58:24 AM7/31/12
to cherryp...@googlegroups.com

The simplest solution is to look at the value of cherrypy.request.method.

My own use of cherrypy is focused on RESTful web services, so I prefer for my request handlers to be organised by request method. Some people like this pattern, others don't. See here: http://tools.cherrypy.org/wiki/HTTPMethodFiltering

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/sO1rVBKgAYQJ.
To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.

Nightfury

unread,
Jul 31, 2012, 5:16:28 AM7/31/12
to cherryp...@googlegroups.com

my problem solved...

class client:
   
    def index(self,name=None):
        request=cherrypy.request
        if request.method=='GET':

                ClientObj = Collection()
                print "name in view",name
           
                ClientObj.name=name
                loginBll.createClient(ClientObj)
        else:

Eric Larson

unread,
Jul 31, 2012, 12:14:41 PM7/31/12
to cherryp...@googlegroups.com
There is also the MethdodDispatcher:

import cherrypy
from pprint import pformat

class Foo(object):

exposed = True

def GET(self, *args, **kw):
# accepting *args so we can use any extra URL segments
# for example try requesting /foo/bar/baz and printing args
return pformat((args, kw))

def POST(self, name=None):
db.save(name)
raise cherrypy.HTTPRedirect(cherrypy.url())


if __name__ == '__main__':
conf = {'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}
cherrypy.tree.mount(Foo(), '/', config=conf)
cherrypy.engine.start()
cherrypy.engine.block()

One thing that was helpful for me when learning cherrypy was that the
*args were the remaining segments of the requested URL. For example, if
I mounted the above instance of foo at "/foo" and requested
"/foo/bar/baz" the *args would contain ['bar', 'baz']. If you want to
move the analysis of *args outside your handler, you can use the
_cp_dispatch[1] method.

Hope that helps!

Eric

[1] https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32







> --
> You received this message because you are subscribed to the Google Groups
> "cherrypy-users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/
> cherrypy-users/-/Lfn4UPYx4mQJ.
Reply all
Reply to author
Forward
0 new messages