Permalink
Browse files

Added example of form validation using the Django forms library, had …

…to settings.configure(USE_I18N=False) to get it to work though
  • Loading branch information...
1 parent 780b498 commit e0f1875b875a62b3b17acd65ad371db9d4ab5aed Simon Willison committed May 12, 2009
Showing with 28 additions and 0 deletions.
  1. +5 −0 djng/__init__.py
  2. +23 −0 example_forms.py
View
@@ -1,3 +1,8 @@
+# Some settings are just too much work to monkey-patch around
+from django.conf import settings
+settings.configure(USE_18N = False)
+del settings
+
from django.conf.urls.defaults import url
from router import Router
from errors import ErrorWrapper
View
@@ -13,6 +13,7 @@ def index(request):
<p><textarea name="text" rows="5" cols="30"></textarea></p>
<p><input type="submit" value="Capitalise text"></p>
</form>
+ <a href="/validate/">Form validation demo</a>
""")
def search(request):
@@ -26,10 +27,32 @@ def submit(request):
text = request.POST.get('text', 'no-text')
return djng.Response(djng.escape(text.upper()))
+class DemoForm(djng.forms.Form):
+ name = djng.forms.CharField(max_length = 100)
+ email = djng.forms.EmailField()
+ optional_text = djng.forms.CharField(required = False)
+
+def validate(request):
+ if request.method == 'POST':
+ form = DemoForm(request.POST)
+ if form.is_valid():
+ return djng.Response('Form was valid: %s' % djng.escape(
+ repr(form.cleaned_data)
+ ))
+ else:
+ form = DemoForm()
+ return djng.Response("""
+ <form action="/validate/" method="post">
+ %s
+ <p><input type="submit">
+ </form>
+ """ % form.as_p())
+
app = djng.Router(
(r'^$', index),
(r'^search/$', search),
(r'^submit/$', submit),
+ (r'^validate/$', validate),
)
if __name__ == '__main__':

0 comments on commit e0f1875

Please sign in to comment.