Combining Tornado Server With Deform Python Code

12 views
Skip to first unread message

James Petullo

unread,
Oct 4, 2017, 3:09:52 PM10/4/17
to pylons-discuss
I am creating a a web application with Tornado; however, I would also like to implement the form capabilities of the pylons package Deform. I have set up my small Tornado framework with the code below:

     
    import tornado.ioloop
    import tornado.web
    import os
 


    class MainHandler(tornado.web.RequestHandler):
            def get(self):

                 self.render('/Users/jamespetullo/Downloads/home.html')

      class StudentRegster(tornado.web.RequestHandler):
           def get(self):
                self.render("/Users/jamespetullo/Downloads/student_register.html")

         def post(self):
              first_name = self.get_argument("first_name")
              last_name = self.get_argument("last_name")
              email = self.get_argument("email")

This is a very simple registration form; however, I would like to implement the deform AJAX registration form as found here:http://deformdemo.repoze.org/ajaxform_redirect/

My question is, how do I implement the code for the Deform login page with my tornado "get" and "post" methods?  Currently, those methods in the code above are merely serving html files. How do I successfully include the deform ajax form code? Is this even possible? If it is possible, what is the best way to go about implementing the deform code?

Here is the Deform AJAX Redirect Form code:

      
    @view_config(renderer='templates/form.pt', name='ajaxform_redirect')
    @demonstrate('AJAX form submission (redirect on success)')
    def ajaxform_redirect(self):

        class Mapping(colander.Schema):
            name = colander.SchemaNode(
                colander.String(),
                description='Content name')
            date = colander.SchemaNode(
                colander.Date(),
                widget=deform.widget.DatePartsWidget(),
                description='Content date')

        class Schema(colander.Schema):
            number = colander.SchemaNode(
                colander.Integer())
            mapping = Mapping()

        schema = Schema()
        options = """
        {success:
          function (rText, sText, xhr, form) {
            var loc = xhr.getResponseHeader('X-Relocate');
            if (loc) {
              document.location = loc;
            };
           }
        }
        """

        def succeed():
            location = self.request.resource_url(
                self.request.root,
                'thanks.html',
                route_name='deformdemo',
                )
            # To appease jquery 1.6+, we need to return something that smells
            # like HTML, or we get a "Node cannot be inserted at the
            # specified point in the hierarchy" Javascript error.  This didn't
            # used to be required under JQuery 1.4.
            return Response(
                '<div>hurr</div>',
                headers=[('X-Relocate', location),
                         ('Content-Type', 'text/html')]
                )

        form = deform.Form(schema, buttons=('submit',), use_ajax=True,
                           ajax_options=options)

        return self.render_form(form, success=succeed)


Any help is appreciated. Thank you!
Reply all
Reply to author
Forward
0 new messages