1 from xml.dom.minidom import parseString |
|
2 from django.template import Context, Template |
|
3 from django.http import HttpResponse, HttpResponseRedirect |
|
4 from django.contrib.auth.decorators import login_required |
|
5 from django.newforms.forms import Form |
|
6 from django.newforms import fields |
|
7 |
|
8 def get_view(request): |
|
9 "A simple view that expects a GET request, and returns a rendered template" |
|
10 t = Template('This is a test. {{ var }} is the value.', name='GET Template') |
|
11 c = Context({'var': 42}) |
|
12 |
|
13 return HttpResponse(t.render(c)) |
|
14 |
|
15 def post_view(request): |
|
16 """A view that expects a POST, and returns a different template depending |
|
17 on whether any POST data is available |
|
18 """ |
|
19 if request.method == 'POST': |
|
20 if request.POST: |
|
21 t = Template('Data received: {{ data }} is the value.', name='POST Template') |
|
22 c = Context({'data': request.POST['value']}) |
|
23 else: |
|
24 t = Template('Viewing POST page.', name='Empty POST Template') |
|
25 c = Context() |
|
26 else: |
|
27 t = Template('Viewing GET page.', name='Empty GET Template') |
|
28 c = Context() |
|
29 |
|
30 return HttpResponse(t.render(c)) |
|
31 |
|
32 def raw_post_view(request): |
|
33 """A view which expects raw XML to be posted and returns content extracted |
|
34 from the XML""" |
|
35 if request.method == 'POST': |
|
36 root = parseString(request.raw_post_data) |
|
37 first_book = root.firstChild.firstChild |
|
38 title, author = [n.firstChild.nodeValue for n in first_book.childNodes] |
|
39 t = Template("{{ title }} - {{ author }}", name="Book template") |
|
40 c = Context({"title": title, "author": author}) |
|
41 else: |
|
42 t = Template("GET request.", name="Book GET template") |
|
43 c = Context() |
|
44 |
|
45 return HttpResponse(t.render(c)) |
|
46 |
|
47 def redirect_view(request): |
|
48 "A view that redirects all requests to the GET view" |
|
49 return HttpResponseRedirect('/test_client/get_view/') |
|
50 |
|
51 TestChoices = ( |
|
52 ('a', 'First Choice'), |
|
53 ('b', 'Second Choice'), |
|
54 ('c', 'Third Choice'), |
|
55 ('d', 'Fourth Choice'), |
|
56 ('e', 'Fifth Choice') |
|
57 ) |
|
58 |
|
59 class TestForm(Form): |
|
60 text = fields.CharField() |
|
61 email = fields.EmailField() |
|
62 value = fields.IntegerField() |
|
63 single = fields.ChoiceField(choices=TestChoices) |
|
64 multi = fields.MultipleChoiceField(choices=TestChoices) |
|
65 |
|
66 def form_view(request): |
|
67 "A view that tests a simple form" |
|
68 if request.method == 'POST': |
|
69 form = TestForm(request.POST) |
|
70 if form.is_valid(): |
|
71 t = Template('Valid POST data.', name='Valid POST Template') |
|
72 c = Context() |
|
73 else: |
|
74 t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template') |
|
75 c = Context({'form': form}) |
|
76 else: |
|
77 form = TestForm() |
|
78 t = Template('Viewing base form. {{ form }}.', name='Form GET Template') |
|
79 c = Context({'form': form}) |
|
80 |
|
81 return HttpResponse(t.render(c)) |
|
82 |
|
83 def login_protected_view(request): |
|
84 "A simple view that is login protected." |
|
85 t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template') |
|
86 c = Context({'user': request.user}) |
|
87 |
|
88 return HttpResponse(t.render(c)) |
|
89 login_protected_view = login_required(login_protected_view) |
|
90 |
|
91 def session_view(request): |
|
92 "A view that modifies the session" |
|
93 request.session['tobacconist'] = 'hovercraft' |
|
94 |
|
95 t = Template('This is a view that modifies the session.', |
|
96 name='Session Modifying View Template') |
|
97 c = Context() |
|
98 return HttpResponse(t.render(c)) |
|
99 |
|
100 def broken_view(request): |
|
101 """A view which just raises an exception, simulating a broken view.""" |
|
102 raise KeyError("Oops! Looks like you wrote some bad code.") |
|