htmlfill and Pylons integration, form validation inside controller and Mako template example with i18n


/ Published in: Python
Save to your folder(s)

These are code snippets from different files for a Pylons project that show a working (provided the environment) htmlfill example.


Copy this code and paste it in your HTML
  1. ######################################################## development.ini / production.ini
  2.  
  3. [app:main]
  4. ...
  5. lang = fr
  6.  
  7. ##################################################################### project/lib/base.py
  8.  
  9. class BaseController(WSGIController):
  10.  
  11. ### [...snip...]
  12.  
  13. def __before__(self):
  14.  
  15. ### [...snip...]
  16.  
  17. formencode.api.set_stdtranslation(domain="FormEncode",
  18. languages=[config.get('lang', 'fr')])
  19.  
  20. ################################################################## project/lib/helpers.py
  21.  
  22. from formencode import htmlfill
  23. def p_error_formatter(error):
  24. return '<p class="error">%s</p>' % htmlfill.html_quote(error)
  25.  
  26. def ol_error_formatter(error):
  27. return '<ol class="errors"><li>%s</li></ol>' % htmlfill.html_quote(error)
  28.  
  29. def div_error_formatter(error):
  30. return '<div class="error">%s</div>' % htmlfill.html_quote(error)
  31.  
  32. def errorform(form_content, errors):
  33. """Return an HTMLFILLed form, according to those errors"""
  34. formats = {'default': p_error_formatter,
  35. 'p': p_error_formatter,
  36. 'ol': ol_error_formatter,
  37. 'div': div_error_formatter}
  38.  
  39. defs = errors.value if errors else {}
  40. errs = errors.unpack_errors() if errors else {}
  41.  
  42. return htmlfill.render(form_content, defs, errs,
  43. add_attributes={'+class': ' error'},
  44. error_formatters=formats)
  45.  
  46. #
  47. # NOTE: this will add a "class='error'" to each <input /> tag also.
  48. ##
  49.  
  50. ############################################################# project/contorller/prefs.py
  51. import formencode
  52. from formencode import validators
  53. from formencode import htmlfill
  54.  
  55. class UniqueUsername(formencode.FancyValidator):
  56. def _to_python(self, value, state):
  57. chk = model.User.query.filter_by(username=value).first()
  58. if chk:
  59. raise formencode.Invalid(
  60. "Ce nom d'usager existe déjà",
  61. value, state)
  62. return value
  63.  
  64.  
  65. class PasswdValidate(formencode.Schema):
  66. username = formencode.All(validators.String(min=6,max=20),
  67. validators.PlainText(),
  68. UniqueUsername())
  69. passwd1 = validators.String(not_empty=True)
  70. passwd2 = validators.String(not_empty=True)
  71.  
  72. chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')]
  73.  
  74.  
  75. ### CONTROLLER
  76.  
  77. class PrefsController(BaseController):
  78. def passwd(self):
  79.  
  80. return render("/prefs/passwd.html")
  81.  
  82. def save_passwd(self):
  83.  
  84. try:
  85. prm = PasswdValidate().to_python(request.params)
  86. except validators.Invalid, e:
  87. c.errors = e
  88. return render('/prefs/passwd.html')
  89.  
  90.  
  91. ##################################### project/templates/prefs/passwd.html (Mako template)
  92. ##
  93. ## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content
  94. ##
  95. ${h.errorform(form(), c.errors)}
  96. <%def name="form()" buffered="True">
  97. <form id="editform" action="${h.url_for(action='save_passwd')}" method="post">
  98.  
  99. <ol>
  100. <li class="first"><label for="username">Username<em>*</em></label>
  101. ${h.text_field('username', size=15)}<br />
  102. <form:error name="username" format="ol" />
  103. </li>
  104.  
  105. <li><label>Password:</label>
  106. <input type="password" name="passwd1" size="15" />
  107. <form:error name="passwd1" />
  108. </li>
  109.  
  110. <li class="first"><label>Punch it again:</label>
  111. <input type="password" name="passwd2" size="15" />
  112. <form:error name="passwd2" />
  113. </li>
  114. </ol>
  115. <br />
  116.  
  117. <input type="submit" value="Save" />
  118.  
  119. </form>
  120. </%def>
  121.  
  122. ################################################################# Add your own stylesheet
  123.  
  124. /* with stuff :) */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.