我正在尝试使用 Tornado 构建一个 Web 应用程序,并希望从 Python 包 Deform 中添加一些样式功能。下面,我试图通过结合 Tornado 和 Deform 来实现一个用户登录表单。但是,正在引发错误: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)
我的主要问题是我无法结合 Tornadoget
方法并提供变形模板。谁能指出我正确的方向?请让我知道您有任何问题。
编辑:
这是完整的回溯:
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)