ui.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # encoding=utf-8
  2. import tornado.escape
  3. import tornado.web
  4. class FormUIModule(tornado.web.UIModule):
  5. def render(self, form, action='', method='post', submit=None, submit_sm=False):
  6. strings = [
  7. '<form action="{action}" method="{method}" class="form-horizontal" role="form">'.format(
  8. action=action, method=method),
  9. self.handler.xsrf_form_html()
  10. ]
  11. for field in form:
  12. error = ' has-error' if len(field.errors) > 0 else ''
  13. strings.append('<div class="form-group'+error+'">')
  14. if field.type == 'BooleanField':
  15. strings.append('<div class="col-sm-offset-2"><div class="checkbox">')
  16. strings.append(field())
  17. strings.append(field.label())
  18. strings.append('</div></div>')
  19. else:
  20. strings.append(field.label(class_='control-label col-sm-2'))
  21. strings.append('<div class="col-sm-10">')
  22. strings.append(field(class_='form-control'))
  23. for error in field.errors:
  24. strings.append('<div class="text-danger">')
  25. strings.append(tornado.escape.xhtml_escape(error))
  26. strings.append('</div>')
  27. strings.append('</div>')
  28. strings.append('</div>')
  29. if submit:
  30. btn_sm = ' btn-sm' if submit_sm else ''
  31. strings.append('<button class="btn btn-primary'+btn_sm+'">')
  32. strings.append(tornado.escape.xhtml_escape(submit))
  33. strings.append('</button>')
  34. strings.append('</form>')
  35. return ''.join(strings)