Implementing Deform With Tornado

116 views Asked by At

I am trying to build a web application with Tornado and would like to add some of the styling features from the Python package Deform. Below, I am trying to implement a user login form by combing both Tornado and Deform. However, an error is being raised: TypeError: __init__() takes exactly 2 arguments (3 given).

class demonstrate(object):
    def __init__(self, title):
       self.title = title

   def __call__(self, method):
       method.demo = self.title
       return method
class MainHandler(tornado.web.RequestHandler):
    def get(self):

    self.render('/Users/jamespetullo/Downloads/studenthost-master/studentscout/home.html')
@view_defaults(route_name='student_register')
class StudentRegster(tornado.web.RequestHandler):
    def __init__(self, request):
       self.request = request
       self.macros = get_renderer('templates/main.pt').implementation().macros
   def render_form(self, form, appstruct=colander.null, submitted='submit',
                success=None, readonly=False, is_i18n=False):

       captured = None

       if submitted in self.request.POST:

           try:
               controls = self.request.POST.items()
               captured = form.validate(controls)
               if success:
                   response = success()
                   if response is not None:
                      return response
               html = form.render(captured)
            except deform.ValidationFailure as e:
                            html = e.render()

       else:
            html = form.render(appstruct, readonly=readonly)

      if self.request.is_xhr:
          return Response(html)

      code, start, end = self.get_code(2)
      locale_name = get_locale_name(self.request)

      reqts = form.get_widget_resources()

      printer = pprint.PrettyPrinter(width=1)
      printer.format = my_safe_repr
      output = printer.pformat(captured)
      captured = highlight(output,
                         PythonLexer(),
                         formatter)

    # values passed to template for rendering
      return {
        'form':html,
        'captured': captured,
        'code': code,
        'start':start,
        'end':end,
        'is_i18n':is_i18n,
        'locale': locale_name,
        'demos':self.get_demos(),
        'title':self.get_title(),
        'css_links':reqts['css'],
        'js_links':reqts['js'],
        }
   @view_config(renderer='templates/form.pt', name='edit')
   @demonstrate('Edit Form')
   def get(self):


       import datetime

       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()
       form = deform.Form(schema, buttons=('submit',))
       appstruct = {
        'number': 42,
        'mapping': {
            'date': datetime.date(2010, 4, 9),
            }
        }

       return self.render_form(form, appstruct=appstruct)

My main issue is that I am unable to combine the Tornado get method and serve the deform templates. Can anyone point me in the right direction? Please let me know of any questions you have.

Edit:

Here is the full traceback:

ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/http1connection.py",   line 238, in _read_message
delegate.finish()
File "/Library/Python/2.7/site-packages/tornado/routing.py", line 251, in finish
self.delegate.finish()
 File "/Library/Python/2.7/site-packages/tornado/web.py", line 2096, in finish
 self.execute()
 File "/Library/Python/2.7/site-packages/tornado/web.py", line 2116, in execute
 **self.handler_kwargs)
 TypeError: __init__() takes exactly 2 arguments (3 given)
1

There are 1 answers

2
Ben Darnell On

If you override __init__, be sure to match the superclass's signature (and call super().__init__(*args, **kw)). In this case RequestHandler.__init__() takes two arguments, the application and request objects. Or if you'd rather not think about matching the superclass's __init__ signature (which you shouldn't really know/care about anyway since these objects are constructed by the framework), you can override initialize() instead (this method is provided by RequestHandler specifically so you don't have to worry about the subtleties of overriding __init__())