I have some HTML forms in my Python Pyramid application. I'd like them to work via AJAX when JavaScript is enabled and when JavaScript is disabled. Now I use different views for AJAX and normal form posts, but the code seems almost the same for these functions, except response. I use view class like this (class body):
def ajax_ok(self, msg):
return Response(body=msg)
def ajax_error(self, msg):
return HTTPInternalServerError(body=msg)
@action(xhr=True, renderer='json', name='ftp')
def ftp_ajax(self):
log.debug('View: %s', 'ftp (ajax)')
if 'form.submitted' in self.params:
try:
self.config.save(self.params)
except:
return self.ajax_error('some error')
else:
return self.ajax_ok('ok')
@action()
def ftp(self):
if 'form.submitted' in self.params:
try:
self.config.save(self.params)
except:
self.request.session.flash('error; ' + msg)
else:
self.request.session.flash('success; ' + msg)
return {'content': render('templates/ftp.pt', {'ftp':
self.config.ftp})}
I need to handle error if any and show them in html. Maybe I should use finished_callback or my own renderer which will send different response for different request type??