本文整理匯總了Python中django.test.utils.ContextList方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.ContextList方法的具體用法?Python utils.ContextList怎麽用?Python utils.ContextList使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.test.utils
的用法示例。
在下文中一共展示了utils.ContextList方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: store_rendered_templates
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def store_rendered_templates(store, signal, sender, template, context, **kwargs):
"""
Stores templates and contexts that are rendered.
The context is copied so that it is an accurate representation at the time
of rendering.
"""
store.setdefault('templates', []).append(template)
store.setdefault('context', ContextList()).append(copy(context))
示例2: store_rendered_templates
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def store_rendered_templates(store, signal, sender, template, context, **kwargs):
"""
Store templates and contexts that are rendered.
The context is copied so that it is an accurate representation at the time
of rendering.
"""
store.setdefault('templates', []).append(template)
if 'context' not in store:
store['context'] = ContextList()
store['context'].append(copy(context))
示例3: clean
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def clean(self, data):
"""Cleans up `data` before using it as a snapshot reference."""
if isinstance(data, RequestContext):
return self.clean(data.flatten())
# XXX: maybe we can do something smarter than blacklisting when we
# have a `ContextList`?
elif isinstance(data, dict) or isinstance(data, ContextList):
return {
key: self.clean(data[key])
for key in data.keys()
if key not in BLACKLISTED_KEYS
}
elif isinstance(data, list):
return [self.clean(item) for item in data]
return data
示例4: store_rendered_templates
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def store_rendered_templates(store, signal, sender, template, context, **kwargs):
"""
Stores templates and contexts that are rendered.
The context is copied so that it is an accurate representation at the time
of rendering.
"""
store.setdefault('templates', []).append(template)
if 'context' not in store:
store['context'] = ContextList()
store['context'].append(copy(context))
示例5: __init__
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def __init__(self, test_case, template_name):
self.test_case = test_case
self.template_name = template_name
self.rendered_templates = []
self.rendered_template_names = []
self.context = ContextList()
示例6: test_inherited_context
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def test_inherited_context(self):
"Context variables can be retrieved from a list of contexts"
response = self.client.get("/request_data_extended/", data={'foo': 'whiz'})
self.assertEqual(response.context.__class__, ContextList)
self.assertEqual(len(response.context), 2)
self.assertIn('get-foo', response.context)
self.assertEqual(response.context['get-foo'], 'whiz')
self.assertEqual(response.context['data'], 'bacon')
with self.assertRaises(KeyError) as cm:
response.context['does-not-exist']
self.assertEqual(cm.exception.args[0], 'does-not-exist')
示例7: test_contextlist_keys
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def test_contextlist_keys(self):
c1 = Context()
c1.update({'hello': 'world', 'goodbye': 'john'})
c1.update({'hello': 'dolly', 'dolly': 'parton'})
c2 = Context()
c2.update({'goodbye': 'world', 'python': 'rocks'})
c2.update({'goodbye': 'dolly'})
k = ContextList([c1, c2])
# None, True and False are builtins of BaseContext, and present
# in every Context without needing to be added.
self.assertEqual({'None', 'True', 'False', 'hello', 'goodbye', 'python', 'dolly'}, k.keys())
示例8: test_contextlist_get
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def test_contextlist_get(self):
c1 = Context({'hello': 'world', 'goodbye': 'john'})
c2 = Context({'goodbye': 'world', 'python': 'rocks'})
k = ContextList([c1, c2])
self.assertEqual(k.get('hello'), 'world')
self.assertEqual(k.get('goodbye'), 'john')
self.assertEqual(k.get('python'), 'rocks')
self.assertEqual(k.get('nonexistent', 'default'), 'default')
示例9: request
# 需要導入模塊: from django.test import utils [as 別名]
# 或者: from django.test.utils import ContextList [as 別名]
def request(self, **request):
"""
The master request method. Composes the environment dictionary
and passes to the handler, returning the result of the handler.
Assumes defaults for the query environment, which can be overridden
using the arguments to the request.
"""
environ = self._base_environ(**request)
# Curry a data dictionary into an instance of the template renderer
# callback function.
data = {}
on_template_render = curry(store_rendered_templates, data)
signals.template_rendered.connect(on_template_render, dispatch_uid="template-render")
# Capture exceptions created by the handler.
got_request_exception.connect(self.store_exc_info, dispatch_uid="request-exception")
try:
try:
response = self.handler(environ)
except TemplateDoesNotExist as e:
# If the view raises an exception, Django will attempt to show
# the 500.html template. If that template is not available,
# we should ignore the error in favor of re-raising the
# underlying exception that caused the 500 error. Any other
# template found to be missing during view error handling
# should be reported as-is.
if e.args != ('500.html',):
raise
# Look for a signalled exception, clear the current context
# exception data, then re-raise the signalled exception.
# Also make sure that the signalled exception is cleared from
# the local cache!
if self.exc_info:
exc_info = self.exc_info
self.exc_info = None
six.reraise(*exc_info)
# Save the client and request that stimulated the response.
response.client = self
response.request = request
# Add any rendered template detail to the response.
response.templates = data.get("templates", [])
response.context = data.get("context")
# Flatten a single context. Not really necessary anymore thanks to
# the __getattr__ flattening in ContextList, but has some edge-case
# backwards-compatibility implications.
if response.context and len(response.context) == 1:
response.context = response.context[0]
# Update persistent cookie data.
if response.cookies:
self.cookies.update(response.cookies)
return response
finally:
signals.template_rendered.disconnect(dispatch_uid="template-render")
got_request_exception.disconnect(dispatch_uid="request-exception")