我试图将Ajax表单提交实现为AJAX Form Submission(inline success)的上下文,但它不适用于我的情况,这是我的代码。
@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:名称'demonst'未定义
如果我删除此行 @demonstrate('AJAX表单提交(内联成功)')
比我给我错误:
AttributeError:'UsersView'对象没有属性'render_form'
需要帮助才能使用deform_bootstrap执行ajax表单提交。
谢谢,
如果我错了,请纠正我。 :)答案 0 :(得分:0)
我有完全相同的问题。问题是表单的render函数实际上并没有成功的关键字参数,即
form.render(success='<div>here</div')
实际上并没有按照你的想法去做。如果您查看演示代码,您会看到他们称之为另一个函数
self.render_form(form, success='<div>here</div>')
这是与表单呈现功能完全独立的功能。它处理调用(让我们假设一个有效的帖子),然后如果验证成功,则检查是否定义了'success'并将其作为HTML返回给响应对象。否则它会在响应对象中返回'form.render'。
希望有所帮助。
基思