当前位置: 首页>>代码示例>>Python>>正文


Python django.DjangoTemplates方法代码示例

本文整理汇总了Python中django.template.backends.django.DjangoTemplates方法的典型用法代码示例。如果您正苦于以下问题:Python django.DjangoTemplates方法的具体用法?Python django.DjangoTemplates怎么用?Python django.DjangoTemplates使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.template.backends.django的用法示例。


在下文中一共展示了django.DjangoTemplates方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_default

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def get_default():
        """
        Return the first DjangoTemplates backend that's configured, or raise
        ImproperlyConfigured if none are configured.

        This is required for preserving historical APIs that rely on a
        globally available, implicitly configured engine such as:

        >>> from django.template import Context, Template
        >>> template = Template("Hello {{ name }}!")
        >>> context = Context({'name': "world"})
        >>> template.render(context)
        'Hello world!'
        """
        # Since Engine is imported in django.template and since
        # DjangoTemplates is a wrapper around this Engine class,
        # local imports are required to avoid import loops.
        from django.template import engines
        from django.template.backends.django import DjangoTemplates
        for engine in engines.all():
            if isinstance(engine, DjangoTemplates):
                return engine.engine
        raise ImproperlyConfigured('No DjangoTemplates backend is configured.') 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:25,代码来源:engine.py

示例2: test_context_has_priority_over_template_context_processors

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_context_has_priority_over_template_context_processors(self):
        # See ticket #23789.
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {
                'context_processors': [test_processor_name],
            },
        })

        template = engine.from_string('{{ processors }}')
        request = RequestFactory().get('/')

        # Context processors run
        content = template.render({}, request)
        self.assertEqual(content, 'yes')

        # Context overrides context processors
        content = template.render({'processors': 'no'}, request)
        self.assertEqual(content, 'no') 
开发者ID:nesdis,项目名称:djongo,代码行数:23,代码来源:test_django.py

示例3: test_render_requires_dict

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_render_requires_dict(self):
        """django.Template.render() requires a dict."""
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {},
        })
        template = engine.from_string('')
        context = Context()
        request_context = RequestContext(RequestFactory().get('/'), {})
        msg = 'context must be a dict rather than Context.'
        with self.assertRaisesMessage(TypeError, msg):
            template.render(context)
        msg = 'context must be a dict rather than RequestContext.'
        with self.assertRaisesMessage(TypeError, msg):
            template.render(request_context) 
开发者ID:nesdis,项目名称:djongo,代码行数:19,代码来源:test_django.py

示例4: test_builtins_discovery

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_builtins_discovery(self):
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {
                'builtins': ['template_backends.apps.good.templatetags.good_tags'],
            },
        })

        self.assertEqual(
            engine.engine.builtins, [
                'django.template.defaulttags',
                'django.template.defaultfilters',
                'django.template.loader_tags',
                'template_backends.apps.good.templatetags.good_tags',
            ]
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:20,代码来源:test_django.py

示例5: test_context_has_priority_over_template_context_processors

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_context_has_priority_over_template_context_processors(self):
        # See ticket #23789.
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {
                'context_processors': [test_processor_name],
            },
        })

        template = engine.from_string('{{ processors }}')
        request = self.request_factory.get('/')

        # Context processors run
        content = template.render({}, request)
        self.assertEqual(content, 'yes')

        # Context overrides context processors
        content = template.render({'processors': 'no'}, request)
        self.assertEqual(content, 'no') 
开发者ID:nesdis,项目名称:djongo,代码行数:23,代码来源:test_django.py

示例6: test_render_requires_dict

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_render_requires_dict(self):
        """django.Template.render() requires a dict."""
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {},
        })
        template = engine.from_string('')
        context = Context()
        request_context = RequestContext(self.request_factory.get('/'), {})
        msg = 'context must be a dict rather than Context.'
        with self.assertRaisesMessage(TypeError, msg):
            template.render(context)
        msg = 'context must be a dict rather than RequestContext.'
        with self.assertRaisesMessage(TypeError, msg):
            template.render(request_context) 
开发者ID:nesdis,项目名称:djongo,代码行数:19,代码来源:test_django.py

示例7: get_default

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def get_default():
        """
        When only one DjangoTemplates backend is configured, returns it.

        Raises ImproperlyConfigured otherwise.

        This is required for preserving historical APIs that rely on a
        globally available, implicitly configured engine such as:

        >>> from django.template import Context, Template
        >>> template = Template("Hello {{ name }}!")
        >>> context = Context({'name': "world"})
        >>> template.render(context)
        'Hello world!'
        """
        # Since Engine is imported in django.template and since
        # DjangoTemplates is a wrapper around this Engine class,
        # local imports are required to avoid import loops.
        from django.template import engines
        from django.template.backends.django import DjangoTemplates
        django_engines = [engine for engine in engines.all()
                          if isinstance(engine, DjangoTemplates)]
        if len(django_engines) == 1:
            # Unwrap the Engine instance inside DjangoTemplates
            return django_engines[0].engine
        elif len(django_engines) == 0:
            raise ImproperlyConfigured(
                "No DjangoTemplates backend is configured.")
        else:
            raise ImproperlyConfigured(
                "Several DjangoTemplates backends are configured. "
                "You must select one explicitly.") 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:34,代码来源:engine.py

示例8: check_context_processors

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def check_context_processors(app_configs, **kwargs):
    errors = []

    if VERSION < (2, 2):
        # The admin.E404 check has been introduced in the Django 2.2 cycle.
        return errors

    for engine in engines.all():
        if isinstance(engine, DjangoTemplates):
            django_templates_instance = engine.engine
            break
    else:
        django_templates_instance = None

    if django_templates_instance:
        if (
            "django.contrib.messages.context_processors.messages"
            not in django_templates_instance.context_processors
            and "admin.E404" not in settings.SILENCED_SYSTEM_CHECKS
        ):
            errors.append(
                checks.Error(
                    "If using 'user_messages.context_processors.messages'"
                    " instead of the official messages context processor"
                    " you have to add 'admin.E404' to SILENCED_SYSTEM_CHECKS.",
                    id="user_messages.E001",
                )
            )

    return errors 
开发者ID:matthiask,项目名称:django-user-messages,代码行数:32,代码来源:apps.py

示例9: get_template

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def get_template():
    if this_module.template is None:
        template_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'request_history.html'
        )
        with open(template_path) as template_file:
            this_module.template = Template(
                template_file.read(),
                engine=DjangoTemplates({'NAME': 'rh', 'DIRS': [], 'APP_DIRS': False, 'OPTIONS': {}}).engine
        )
    return this_module.template 
开发者ID:djsutho,项目名称:django-debug-toolbar-request-history,代码行数:14,代码来源:request_history.py

示例10: test_templatetag_discovery

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_templatetag_discovery(self):
        engine = DjangoTemplates({
            'DIRS': [],
            'APP_DIRS': False,
            'NAME': 'django',
            'OPTIONS': {
                'libraries': {
                    'alternate': 'template_backends.apps.good.templatetags.good_tags',
                    'override': 'template_backends.apps.good.templatetags.good_tags',
                },
            },
        })

        # libraries are discovered from installed applications
        self.assertEqual(
            engine.engine.libraries['good_tags'],
            'template_backends.apps.good.templatetags.good_tags',
        )
        self.assertEqual(
            engine.engine.libraries['subpackage.tags'],
            'template_backends.apps.good.templatetags.subpackage.tags',
        )
        # libraries are discovered from django.templatetags
        self.assertEqual(
            engine.engine.libraries['static'],
            'django.templatetags.static',
        )
        # libraries passed in OPTIONS are registered
        self.assertEqual(
            engine.engine.libraries['alternate'],
            'template_backends.apps.good.templatetags.good_tags',
        )
        # libraries passed in OPTIONS take precedence over discovered ones
        self.assertEqual(
            engine.engine.libraries['override'],
            'template_backends.apps.good.templatetags.good_tags',
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:39,代码来源:test_django.py

示例11: test_templatetag_discovery_import_error

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_templatetag_discovery_import_error(self):
        """
        Import errors in tag modules should be reraised with a helpful message.
        """
        with self.assertRaisesMessage(
            InvalidTemplateLibrary,
            "ImportError raised when trying to load "
            "'template_backends.apps.importerror.templatetags.broken_tags'"
        ):
            DjangoTemplates({
                'DIRS': [],
                'APP_DIRS': False,
                'NAME': 'django',
                'OPTIONS': {},
            }) 
开发者ID:nesdis,项目名称:djongo,代码行数:17,代码来源:test_django.py

示例12: test_autoescape_default

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_autoescape_default(self):
        templates = [{
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
        }]
        engines = EngineHandler(templates=templates)
        self.assertEqual(
            engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
            'Hello, Bob &amp; Jim'
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:11,代码来源:test_django.py

示例13: test_non_debug_default_template_loaders

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_non_debug_default_template_loaders(self):
        engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
        self.assertEqual(engine.engine.loaders, [('django.template.loaders.cached.Loader', self.default_loaders)]) 
开发者ID:nesdis,项目名称:djongo,代码行数:5,代码来源:test_django.py

示例14: test_debug_default_template_loaders

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_debug_default_template_loaders(self):
        engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
        self.assertEqual(engine.engine.loaders, self.default_loaders) 
开发者ID:nesdis,项目名称:djongo,代码行数:5,代码来源:test_django.py

示例15: test_autoescape_off

# 需要导入模块: from django.template.backends import django [as 别名]
# 或者: from django.template.backends.django import DjangoTemplates [as 别名]
def test_autoescape_off(self):
        templates = [{
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'OPTIONS': {'autoescape': False},
        }]
        engines = EngineHandler(templates=templates)
        self.assertEqual(
            engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
            'Hello, Bob & Jim'
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:12,代码来源:test_django.py


注:本文中的django.template.backends.django.DjangoTemplates方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。