diff --git a/common/lib/mitxmako/shortcuts.py b/common/lib/mitxmako/shortcuts.py index 9f6044b81e..72a9b81e3c 100644 --- a/common/lib/mitxmako/shortcuts.py +++ b/common/lib/mitxmako/shortcuts.py @@ -12,13 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + +log = logging.getLogger("mitx.common.lib.mitxmako") + from django.template import Context from django.http import HttpResponse from . import middleware from django.conf import settings - def render_to_string(template_name, dictionary, context=None, namespace='main'): context_instance = Context(dictionary) # add dictionary to context_instance @@ -27,8 +30,11 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): context_dictionary = {} context_instance['settings'] = settings context_instance['MITX_ROOT_URL'] = settings.MITX_ROOT_URL - for d in middleware.requestcontext: - context_dictionary.update(d) + + # In various testing contexts, there might not be a current request context. + if middleware.requestcontext is not None: + for d in middleware.requestcontext: + context_dictionary.update(d) for d in context_instance: context_dictionary.update(d) if context: