我試圖實現Ajax表單提交作爲上下文給定AJAX Form Submission(inline success)但它不適用於我的情況,這是我的代碼。無法使用deform_bootstrap實現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={}))
輸出: 表單提交的代碼被執行,但在屏幕上的形式保持不變,但它並沒有執行成功()方法。 :(
如果用完全相同的情況下嘗試,因爲在上面的鏈接,例如規定爲
@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:名字 '證明' 沒有定義
如果我刪除此行 @demonstrate('AJAX表單提交(內聯成功)')
比我給我錯誤:
AttributeError的: 'UsersView' 對象有沒有屬性 'render_form'
需要幫助使用deform_bootstrap執行AJAX表單提交。
謝謝,
糾正我,如果我錯了某處。 :)