Skip to content

Commit

Permalink
Added example of form validation using the Django forms library, had …
Browse files Browse the repository at this point in the history
…to settings.configure(USE_I18N=False) to get it to work though
  • Loading branch information
Simon Willison committed May 12, 2009
1 parent 780b498 commit e0f1875
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions djng/__init__.py
@@ -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
Expand Down
23 changes: 23 additions & 0 deletions example_forms.py
Expand Up @@ -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):
Expand All @@ -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__':
Expand Down

0 comments on commit e0f1875

Please sign in to comment.