我正在尝试使用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)
我的主要问题是我无法合并Tornado get
方法并提供变形模板。谁能指出我正确的方向?如果您有任何问题,请告诉我。
编辑:
以下是完整的追溯:
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)
答案 0 :(得分:1)
如果覆盖__init__
,请确保匹配超类的签名(并调用super().__init__(*args, **kw)
)。在这种情况下,RequestHandler.__init__()
接受两个参数,即应用程序和请求对象。或者,如果你不想考虑匹配超类的__init__
签名(你不应该真正知道/关心,因为这些对象是由框架构建的),你可以覆盖initialize()
(此方法由RequestHandler
专门提供,因此您无需担心覆盖__init__()
)的微妙之处