我试图将 Ajax 表单提交实现为给定AJAX 表单提交(内联成功)的上下文,但在我的情况下它不起作用,这是我的代码。
@view_config(route_name='add_user', permission='admin', renderer='add_user.mako')
def add_user(self):
#adding succeed method as per given context in above link.
def succeed():
return Response('<div id="thanks">Thanks!</div>')
schema = UserSchema(validator = user_DoesExist).bind(request=self.request)
#Form defined with use_ajax=True.
form = deform.Form(schema, action=self.request.route_url('add_user'), buttons=('Add User','Cancel'), use_ajax=True)
if 'Cancel' in self.request.params:
return HTTPFound(location = self.request.route_url('home'))
if 'Add_User' in self.request.params:
appstruct = None
try:
appstruct = form.validate(self.request.POST.items())
except deform.ValidationFailure, e:
log.exception('in form validated')
return {'form':e.render()}
newuser = User(username=appstruct['username'],
name=appstruct['name'],
extension=appstruct['extension'],
pin=appstruct['pin'],
status=1)
DBSession.add(newuser)
DBSession.flush()
return dict(form=form.render(success=succeed))
return dict(form=form.render(appstruct={}))
输出: 表单提交代码被执行,但屏幕上的表单保持不变,但它不执行succeed()方法。:(
如果尝试使用与上述链接中所述完全相同的上下文,例如
@view_config(route_name='add_user', permission='admin', renderer='add_user.mako', name='ajaxform')
@demonstrate('AJAX form submission (inline success)')
def ajaxform(self):
#adding succeed method as per given context in above link.
def succeed():
return Response('<div id="thanks">Thanks!</div>')
schema = UserSchema(validator = user_DoesExist).bind(request=self.request)
#Form defined with use_ajax=True.
form = deform.Form(schema, action=self.request.route_url('add_user'), buttons=('Add User','Cancel'), use_ajax=True)
if 'Cancel' in self.request.params:
return HTTPFound(location = self.request.route_url('home'))
if 'Add_User' in self.request.params:
appstruct = None
try:
appstruct = form.validate(self.request.POST.items())
except deform.ValidationFailure, e:
log.exception('in form validated')
return {'form':e.render()}
newuser = User(username=appstruct['username'],
name=appstruct['name'],
extension=appstruct['extension'],
pin=appstruct['pin'],
status=1)
DBSession.add(newuser)
DBSession.flush()
return self.render_form(form, success=succeed)
return dict(form=form.render(appstruct={}))
输出: NameError: name 'demonstrate' is not defined
如果我删除这一行 @demonstrate('AJAX 表单提交(内联成功)')
比我给我的错误:
AttributeError:“UsersView”对象没有属性“render_form”
需要帮助来使用变形引导程序执行 ajax 表单提交。
谢谢,
如果我在某处错了,请纠正我。:)