img/doc/tutorial -> img/doc/tutorial-trunk in docs/tutorial02.txt
[django.git] / examples / hello / views.py
blob07f955a38d7dabe8d4864178371bde15190923d2
1 from django.http import HttpResponse
2 from django.utils.html import escape
4 def hello_html(request):
5 "This view is a basic 'hello world' example in HTML."
6 return HttpResponse('<h1>Hello, world.</h1>')
8 def hello_text(request):
9 "This view is a basic 'hello world' example in plain text."
10 return HttpResponse('Hello, world.', mimetype='text/plain')
12 def hello_write(request):
13 "This view demonstrates how an HttpResponse object has a write() method."
14 r = HttpResponse()
15 r.write("<p>Here's a paragraph.</p>")
16 r.write("<p>Here's another paragraph.</p>")
17 return r
19 def metadata(request):
20 "This view demonstrates how to retrieve request metadata, such as HTTP headers."
21 r = HttpResponse('<h1>All about you</h1>')
22 r.write("<p>Here's all known metadata about your request, according to <code>request.META</code>:</p>")
23 r.write('<table>')
24 meta_items = request.META.items()
25 meta_items.sort()
26 for k, v in meta_items:
27 r.write('<tr><th>%s</th><td>%r</td></tr>' % (k, v))
28 r.write('</table>')
29 return r
31 def get_data(request):
32 "This view demonstrates how to retrieve GET data."
33 r = HttpResponse()
34 if request.GET:
35 r.write('<p>GET data found! Here it is:</p>')
36 r.write('<ul>%s</ul>' % ''.join(['<li><strong>%s:</strong> %r</li>' % (escape(k), escape(v)) for k, v in request.GET.items()]))
37 r.write('<form action="" method="get">')
38 r.write('<p>First name: <input type="text" name="first_name"></p>')
39 r.write('<p>Last name: <input type="text" name="last_name"></p>')
40 r.write('<p><input type="submit" value="Submit"></p>')
41 r.write('</form>')
42 return r
44 def post_data(request):
45 "This view demonstrates how to retrieve POST data."
46 r = HttpResponse()
47 if request.POST:
48 r.write('<p>POST data found! Here it is:</p>')
49 r.write('<ul>%s</ul>' % ''.join(['<li><strong>%s:</strong> %r</li>' % (escape(k), escape(v)) for k, v in request.POST.items()]))
50 r.write('<form action="" method="post">')
51 r.write('<p>First name: <input type="text" name="first_name"></p>')
52 r.write('<p>Last name: <input type="text" name="last_name"></p>')
53 r.write('<p><input type="submit" value="Submit"></p>')
54 r.write('</form>')
55 return r