Esempio n. 1
0
 def render_to_string(template_name,
                      context=None,
                      request=None,
                      using=None):
     assert using is None, "Multiple template engines required Django >= 1.8"
     return _render_to_string(template_name, context,
                              RequestContext(request))
Esempio n. 2
0
def render_to_string(template_name, context=None, request=None, using=None):
    """Render a template with a given name to a string.

    This will invoke the original
    :py:func:`~django.template.loader.render_to_string` with the correct
    arguments for the active version of Django. It differs a bit from the
    modern version of the function in that it allows a
    :py:class:`~django.template.Context` argument, which it will convert to
    the correct type.

    Args:
        template_name (unicode):
            The name of the template to render.

        context (object, optional):
            The context data as a dictionary or as a
            :py:class:`~django.template.Context`.

        request (django.http.HttpRequest, optional):
            The HTTP request for the render. If specified, a
            :py:class:`~django.template.RequestContext` will be used.

        using (unicode, optional):
            The optional name of a template engine to use for the render.
            This is only supported on Django 1.8 and higher.

    Returns:
        django.utils.safestring.SafeText:
        The resulting rendered content.
    """
    if template_engines is None:
        if not isinstance(context, Context):
            if request:
                context = RequestContext(request, context)
            else:
                context = Context(context)

        return _render_to_string(template_name, context)
    else:
        if isinstance(context, Context):
            context = context.flatten()

        return _render_to_string(template_name=template_name,
                                 context=context,
                                 request=request,
                                 using=using)
Esempio n. 3
0
 def render_to_string(template_name,
                      context=None,
                      request=None,
                      using=None):
     assert (using is None,
             "Multiple template engines requires Django >= 1.8")
     assert (request is None,
             "Using the request keyword argument requires Django >= 1.8")
     return _render_to_string(template_name, context)
Esempio n. 4
0
 def render_to_string(
         template_name, context=None, request=None, using=None):
     assert (
         using is None,
         "Multiple template engines requires Django >= 1.8"
     )  # flake8: noqa
     assert (
         request is None,
         "Using the request keyword argument requires Django >= 1.8"
     )  # flake8: noqa
     return _render_to_string(template_name, context)
Esempio n. 5
0
def render_html_base(object,
                     template_name=None,
                     context=None,
                     object_context_name=None):
    """
    Super-like end point for generic render implementations.
    """

    # Prepare context
    context = dict(context or ())
    context.setdefault(object_context_name or 'object', object)
    if object_context_name is None:
        object_context_name = type(object).__name__.lower()
        context.setdefault(object_context_name, object)

    # Choose template and render
    if template_name is None:
        template_name = render_registry.get(type(object), [
            'render/%s.jinja2' % object_context_name, 'render/default.jinja2'
        ])

    return _render_to_string(template_name, context=context)
Esempio n. 6
0
def render_to_string(request,
                     result,
                     default_template,
                     prefix=None,
                     template_ext='html'):
    templates = [default_template]
    dictionary = None

    # 参数解析
    # {'var': value ...}
    if isinstance(result, dict):
        dictionary = result

    # 'template' or '/root_template'
    elif isinstance(result, basestring):
        templates = [result]

    # 'template1', 'template2' ...
    # 'template', {'var': value ...}
    # 'template1', 'template2', ... {'var': value ...}
    elif isinstance(result, tuple):
        # 最后一项是否为字典
        if isinstance(result[-1], dict):
            templates = list(result[:-1])
            dictionary = result[-1]
        else:
            templates = list(result)

    if getattr(request, 'is_mobile', False):
        templates = [t + '.mobile' for t in templates] + templates

    for i in xrange(0, len(templates)):
        if templates[i].startswith('/'):
            templates[i] = templates[i][1:]
        elif prefix:
            templates[i] = prefix + templates[i]
        templates[i] += '.' + template_ext

    return _render_to_string(templates, dictionary, RequestContext(request))
Esempio n. 7
0
 def render_to_string(template_name, context=None, request=None, using=None):
     assert using is None, "Multiple template engines required Django >= 1.8"
     return _render_to_string(template_name, context, RequestContext(request))