-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_forms.py
59 lines (52 loc) · 1.56 KB
/
example_forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import djng
def index(request):
return djng.Response("""
<h1>Forms demo</h1>
<form action="/search/" method="get">
<p>
<input type="search" name="q">
<input type="submit" value="Search">
</p>
</form>
<form action="/submit/" method="post">
<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):
return djng.Response(
"This page would search for %s" % djng.escape(
request.GET.get('q', 'no-search-term')
)
)
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__':
djng.serve(app, '0.0.0.0', 8888)