2018-02-05 10:22:24 +00:00
|
|
|
import json
|
2017-01-07 11:11:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2007-02-17 00:23:09 +00:00
|
|
|
from xml.dom.minidom import parseString
|
2007-09-04 00:50:06 +00:00
|
|
|
|
2007-11-07 22:45:07 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2011-10-13 18:04:12 +00:00
|
|
|
from django.core import mail
|
2008-07-19 01:22:26 +00:00
|
|
|
from django.forms import fields
|
2013-02-08 23:45:26 +00:00
|
|
|
from django.forms.forms import Form, ValidationError
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.forms.formsets import BaseFormSet, formset_factory
|
2014-10-13 11:10:00 +00:00
|
|
|
from django.http import (
|
2015-01-28 12:35:27 +00:00
|
|
|
HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed,
|
|
|
|
HttpResponseNotFound, HttpResponseRedirect,
|
2014-10-13 11:10:00 +00:00
|
|
|
)
|
2015-12-22 18:50:16 +00:00
|
|
|
from django.shortcuts import render
|
2011-10-13 18:04:12 +00:00
|
|
|
from django.template import Context, Template
|
2015-08-07 19:28:54 +00:00
|
|
|
from django.test import Client
|
2010-02-09 15:02:39 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2007-05-07 12:34:18 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def get_view(request):
|
|
|
|
"A simple view that expects a GET request, and returns a rendered template"
|
|
|
|
t = Template('This is a test. {{ var }} is the value.', name='GET Template')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
c = Context({'var': request.GET.get('var', 42)})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-10-13 11:10:00 +00:00
|
|
|
def trace_view(request):
|
|
|
|
"""
|
|
|
|
A simple view that expects a TRACE request and echoes its status line.
|
|
|
|
|
|
|
|
TRACE requests should not have an entity; the view will return a 400 status
|
|
|
|
response if it is present.
|
|
|
|
"""
|
|
|
|
if request.method.upper() != "TRACE":
|
|
|
|
return HttpResponseNotAllowed("TRACE")
|
|
|
|
elif request.body:
|
|
|
|
return HttpResponseBadRequest("TRACE requests MUST NOT include an entity")
|
|
|
|
else:
|
|
|
|
protocol = request.META["SERVER_PROTOCOL"]
|
|
|
|
t = Template(
|
|
|
|
'{{ method }} {{ uri }} {{ version }}',
|
|
|
|
name="TRACE Template",
|
|
|
|
)
|
|
|
|
c = Context({
|
|
|
|
'method': request.method,
|
|
|
|
'uri': request.path,
|
|
|
|
'version': protocol,
|
|
|
|
})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
|
|
|
|
2017-05-22 10:49:39 +00:00
|
|
|
def put_view(request):
|
|
|
|
if request.method == 'PUT':
|
|
|
|
t = Template('Data received: {{ data }} is the body.', name='PUT Template')
|
2018-02-12 15:50:43 +00:00
|
|
|
c = Context({
|
|
|
|
'Content-Length': request.META['CONTENT_LENGTH'],
|
|
|
|
'data': request.body.decode(),
|
|
|
|
})
|
2017-05-22 10:49:39 +00:00
|
|
|
else:
|
|
|
|
t = Template('Viewing GET page.', name='Empty GET Template')
|
|
|
|
c = Context()
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def post_view(request):
|
|
|
|
"""A view that expects a POST, and returns a different template depending
|
|
|
|
on whether any POST data is available
|
|
|
|
"""
|
2007-02-17 00:23:09 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if request.POST:
|
|
|
|
t = Template('Data received: {{ data }} is the value.', name='POST Template')
|
|
|
|
c = Context({'data': request.POST['value']})
|
|
|
|
else:
|
|
|
|
t = Template('Viewing POST page.', name='Empty POST Template')
|
|
|
|
c = Context()
|
2006-09-02 09:34:40 +00:00
|
|
|
else:
|
2007-02-17 00:23:09 +00:00
|
|
|
t = Template('Viewing GET page.', name='Empty GET Template')
|
2006-09-02 09:34:40 +00:00
|
|
|
c = Context()
|
2018-02-05 10:22:24 +00:00
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
|
|
|
|
|
|
|
def json_view(request):
|
|
|
|
"""
|
|
|
|
A view that expects a request with the header 'application/json' and JSON
|
2018-08-18 12:15:24 +00:00
|
|
|
data, which is deserialized and included in the context.
|
2018-02-05 10:22:24 +00:00
|
|
|
"""
|
|
|
|
if request.META.get('CONTENT_TYPE') != 'application/json':
|
|
|
|
return HttpResponse()
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2018-02-05 10:22:24 +00:00
|
|
|
t = Template('Viewing {} page. With data {{ data }}.'.format(request.method))
|
|
|
|
data = json.loads(request.body.decode('utf-8'))
|
2018-08-18 12:15:24 +00:00
|
|
|
c = Context({'data': data})
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2008-07-12 06:16:42 +00:00
|
|
|
def view_with_header(request):
|
|
|
|
"A view that has a custom header"
|
|
|
|
response = HttpResponse()
|
|
|
|
response['X-DJANGO-TEST'] = 'Slartibartfast'
|
|
|
|
return response
|
2010-10-12 14:06:11 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-17 00:23:09 +00:00
|
|
|
def raw_post_view(request):
|
|
|
|
"""A view which expects raw XML to be posted and returns content extracted
|
|
|
|
from the XML"""
|
|
|
|
if request.method == 'POST':
|
2011-12-16 23:40:32 +00:00
|
|
|
root = parseString(request.body)
|
2007-02-17 00:23:09 +00:00
|
|
|
first_book = root.firstChild.firstChild
|
|
|
|
title, author = [n.firstChild.nodeValue for n in first_book.childNodes]
|
|
|
|
t = Template("{{ title }} - {{ author }}", name="Book template")
|
|
|
|
c = Context({"title": title, "author": author})
|
|
|
|
else:
|
|
|
|
t = Template("GET request.", name="Book GET template")
|
|
|
|
c = Context()
|
|
|
|
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def redirect_view(request):
|
|
|
|
"A view that redirects all requests to the GET view"
|
2007-08-31 11:37:28 +00:00
|
|
|
if request.GET:
|
|
|
|
query = '?' + urlencode(request.GET, True)
|
|
|
|
else:
|
|
|
|
query = ''
|
2014-01-14 15:43:27 +00:00
|
|
|
return HttpResponseRedirect('/get_view/' + query)
|
2007-03-22 10:23:52 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2017-05-22 10:49:39 +00:00
|
|
|
def _post_view_redirect(request, status_code):
|
|
|
|
"""Redirect to /post_view/ using the status code."""
|
|
|
|
redirect_to = request.GET.get('to', '/post_view/')
|
|
|
|
return HttpResponseRedirect(redirect_to, status=status_code)
|
|
|
|
|
|
|
|
|
|
|
|
def method_saving_307_redirect_view(request):
|
|
|
|
return _post_view_redirect(request, 307)
|
|
|
|
|
|
|
|
|
|
|
|
def method_saving_308_redirect_view(request):
|
|
|
|
return _post_view_redirect(request, 308)
|
|
|
|
|
|
|
|
|
2010-02-13 12:01:14 +00:00
|
|
|
def view_with_secure(request):
|
|
|
|
"A view that indicates if the request was secure"
|
|
|
|
response = HttpResponse()
|
|
|
|
response.test_was_secure_request = request.is_secure()
|
2013-10-28 14:00:54 +00:00
|
|
|
response.test_server_port = request.META.get('SERVER_PORT', 80)
|
2010-02-13 12:01:14 +00:00
|
|
|
return response
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-05-10 11:27:59 +00:00
|
|
|
def double_redirect_view(request):
|
|
|
|
"A view that redirects all requests to a redirection view"
|
2014-01-14 15:43:27 +00:00
|
|
|
return HttpResponseRedirect('/permanent_redirect_view/')
|
2007-05-10 11:27:59 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-05-10 11:27:59 +00:00
|
|
|
def bad_view(request):
|
|
|
|
"A view that returns a 404 with some error content"
|
|
|
|
return HttpResponseNotFound('Not found!. This page contains some MAGIC content')
|
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
TestChoices = (
|
|
|
|
('a', 'First Choice'),
|
|
|
|
('b', 'Second Choice'),
|
|
|
|
('c', 'Third Choice'),
|
|
|
|
('d', 'Fourth Choice'),
|
|
|
|
('e', 'Fifth Choice')
|
|
|
|
)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
class TestForm(Form):
|
|
|
|
text = fields.CharField()
|
|
|
|
email = fields.EmailField()
|
|
|
|
value = fields.IntegerField()
|
|
|
|
single = fields.ChoiceField(choices=TestChoices)
|
|
|
|
multi = fields.MultipleChoiceField(choices=TestChoices)
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2013-02-08 23:45:26 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
if cleaned_data.get("text") == "Raise non-field error":
|
|
|
|
raise ValidationError("Non-field error.")
|
|
|
|
return cleaned_data
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
def form_view(request):
|
|
|
|
"A view that tests a simple form"
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = TestForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
t = Template('Valid POST data.', name='Valid POST Template')
|
|
|
|
c = Context()
|
|
|
|
else:
|
|
|
|
t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template')
|
|
|
|
c = Context({'form': form})
|
|
|
|
else:
|
2007-07-21 05:17:20 +00:00
|
|
|
form = TestForm(request.GET)
|
2007-03-22 10:23:52 +00:00
|
|
|
t = Template('Viewing base form. {{ form }}.', name='Form GET Template')
|
|
|
|
c = Context({'form': form})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-05-07 12:34:18 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-05-07 12:34:18 +00:00
|
|
|
def form_view_with_template(request):
|
|
|
|
"A view that tests a simple form"
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = TestForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
message = 'POST data OK'
|
|
|
|
else:
|
|
|
|
message = 'POST data has errors'
|
|
|
|
else:
|
|
|
|
form = TestForm()
|
|
|
|
message = 'GET form page'
|
2015-12-22 18:50:16 +00:00
|
|
|
return render(request, 'form_view.html', {
|
|
|
|
'form': form,
|
|
|
|
'message': message,
|
|
|
|
})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2013-02-08 23:45:26 +00:00
|
|
|
class BaseTestFormSet(BaseFormSet):
|
|
|
|
def clean(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""No two email addresses are the same."""
|
2013-02-08 23:45:26 +00:00
|
|
|
if any(self.errors):
|
|
|
|
# Don't bother validating the formset unless each form is valid
|
|
|
|
return
|
|
|
|
|
|
|
|
emails = []
|
|
|
|
for i in range(0, self.total_form_count()):
|
|
|
|
form = self.forms[i]
|
|
|
|
email = form.cleaned_data['email']
|
|
|
|
if email in emails:
|
|
|
|
raise ValidationError(
|
|
|
|
"Forms in a set must have distinct email addresses."
|
|
|
|
)
|
|
|
|
emails.append(email)
|
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2013-02-08 23:45:26 +00:00
|
|
|
TestFormSet = formset_factory(TestForm, BaseTestFormSet)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2013-02-08 23:45:26 +00:00
|
|
|
def formset_view(request):
|
|
|
|
"A view that tests a simple formset"
|
|
|
|
if request.method == 'POST':
|
|
|
|
formset = TestFormSet(request.POST)
|
|
|
|
if formset.is_valid():
|
|
|
|
t = Template('Valid POST data.', name='Valid POST Template')
|
|
|
|
c = Context()
|
|
|
|
else:
|
|
|
|
t = Template('Invalid POST data. {{ my_formset.errors }}',
|
|
|
|
name='Invalid POST Template')
|
|
|
|
c = Context({'my_formset': formset})
|
|
|
|
else:
|
|
|
|
formset = TestForm(request.GET)
|
|
|
|
t = Template('Viewing base formset. {{ my_formset }}.',
|
|
|
|
name='Formset GET Template')
|
|
|
|
c = Context({'my_formset': formset})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-11-14 17:20:44 +00:00
|
|
|
@login_required
|
2006-09-02 09:34:40 +00:00
|
|
|
def login_protected_view(request):
|
|
|
|
"A simple view that is login protected."
|
|
|
|
t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template')
|
|
|
|
c = Context({'user': request.user})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-02-09 13:47:36 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2016-11-14 17:20:44 +00:00
|
|
|
@login_required(redirect_field_name='redirect_to')
|
2007-09-14 19:25:37 +00:00
|
|
|
def login_protected_view_changed_redirect(request):
|
|
|
|
"A simple view that is login protected with a custom redirect field set"
|
|
|
|
t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template')
|
|
|
|
c = Context({'user': request.user})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-08-12 14:15:41 +00:00
|
|
|
def _permission_protected_view(request):
|
2007-11-07 22:45:07 +00:00
|
|
|
"A simple view that is permission protected."
|
|
|
|
t = Template('This is a permission protected test. '
|
|
|
|
'Username is {{ user.username }}. '
|
2013-10-27 01:27:42 +00:00
|
|
|
'Permissions are {{ user.get_all_permissions }}.',
|
2007-11-07 22:45:07 +00:00
|
|
|
name='Permissions Template')
|
|
|
|
c = Context({'user': request.user})
|
|
|
|
return HttpResponse(t.render(c))
|
2016-11-12 17:11:23 +00:00
|
|
|
|
|
|
|
|
2013-03-03 21:44:06 +00:00
|
|
|
permission_protected_view = permission_required('permission_not_granted')(_permission_protected_view)
|
2015-09-11 23:33:12 +00:00
|
|
|
permission_protected_view_exception = (
|
|
|
|
permission_required('permission_not_granted', raise_exception=True)(_permission_protected_view)
|
|
|
|
)
|
2007-11-07 22:45:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class _ViewManager:
|
2010-02-09 15:02:39 +00:00
|
|
|
@method_decorator(login_required)
|
2007-11-07 22:45:07 +00:00
|
|
|
def login_protected_view(self, request):
|
|
|
|
t = Template('This is a login protected test using a method. '
|
|
|
|
'Username is {{ user.username }}.',
|
|
|
|
name='Login Method Template')
|
|
|
|
c = Context({'user': request.user})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-03-03 21:44:06 +00:00
|
|
|
@method_decorator(permission_required('permission_not_granted'))
|
2007-11-07 22:45:07 +00:00
|
|
|
def permission_protected_view(self, request):
|
|
|
|
t = Template('This is a permission protected test using a method. '
|
|
|
|
'Username is {{ user.username }}. '
|
2013-10-27 01:27:42 +00:00
|
|
|
'Permissions are {{ user.get_all_permissions }}.',
|
2007-11-07 22:45:07 +00:00
|
|
|
name='Permissions Template')
|
|
|
|
c = Context({'user': request.user})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2007-11-07 22:45:07 +00:00
|
|
|
_view_manager = _ViewManager()
|
|
|
|
login_protected_method_view = _view_manager.login_protected_view
|
|
|
|
permission_protected_method_view = _view_manager.permission_protected_view
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-09 13:47:36 +00:00
|
|
|
def session_view(request):
|
|
|
|
"A view that modifies the session"
|
|
|
|
request.session['tobacconist'] = 'hovercraft'
|
2007-09-04 00:54:40 +00:00
|
|
|
|
|
|
|
t = Template('This is a view that modifies the session.',
|
2007-02-09 13:47:36 +00:00
|
|
|
name='Session Modifying View Template')
|
|
|
|
c = Context()
|
|
|
|
return HttpResponse(t.render(c))
|
2007-02-11 00:23:31 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-11 00:23:31 +00:00
|
|
|
def broken_view(request):
|
|
|
|
"""A view which just raises an exception, simulating a broken view."""
|
|
|
|
raise KeyError("Oops! Looks like you wrote some bad code.")
|
2007-05-08 11:19:34 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
def mail_sending_view(request):
|
2010-10-12 14:06:11 +00:00
|
|
|
mail.EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
"Test message",
|
|
|
|
"This is a test email",
|
|
|
|
"from@example.com",
|
2007-05-08 11:19:34 +00:00
|
|
|
['first@example.com', 'second@example.com']).send()
|
|
|
|
return HttpResponse("Mail sent")
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
def mass_mail_sending_view(request):
|
2010-10-12 14:06:11 +00:00
|
|
|
m1 = mail.EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
'First Test message',
|
|
|
|
'This is the first test email',
|
|
|
|
'from@example.com',
|
2007-05-08 11:19:34 +00:00
|
|
|
['first@example.com', 'second@example.com'])
|
2010-10-12 14:06:11 +00:00
|
|
|
m2 = mail.EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
'Second Test message',
|
|
|
|
'This is the second test email',
|
|
|
|
'from@example.com',
|
2007-05-08 11:19:34 +00:00
|
|
|
['second@example.com', 'third@example.com'])
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2010-10-12 14:06:11 +00:00
|
|
|
c = mail.get_connection()
|
2013-10-26 19:15:03 +00:00
|
|
|
c.send_messages([m1, m2])
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
return HttpResponse("Mail sent")
|
2013-09-07 21:13:57 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2015-08-07 19:28:54 +00:00
|
|
|
def nesting_exception_view(request):
|
|
|
|
"""
|
|
|
|
A view that uses a nested client to call another view and then raises an
|
|
|
|
exception.
|
|
|
|
"""
|
|
|
|
client = Client()
|
|
|
|
client.get('/get_view/')
|
|
|
|
raise Exception('exception message')
|
|
|
|
|
|
|
|
|
2013-09-07 21:13:57 +00:00
|
|
|
def django_project_redirect(request):
|
|
|
|
return HttpResponseRedirect('https://www.djangoproject.com/')
|
2016-09-11 09:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upload_view(request):
|
|
|
|
"""Prints keys of request.FILES to the response."""
|
2017-05-27 23:08:46 +00:00
|
|
|
return HttpResponse(', '.join(request.FILES))
|
2017-11-23 16:18:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TwoArgException(Exception):
|
|
|
|
def __init__(self, one, two):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def two_arg_exception(request):
|
|
|
|
raise TwoArgException('one', 'two')
|