English 中文(简体)
Django 1. 4 自定义模板标签返回 HTML
原标题:Django 1.4 custom template tag return HTML

我设置了一个实用的自定义模板标签, 它已经注册, 我可以调用它, 我可以调用它, 它会即时一个模板。 node 例和调用它的 make () 方法。 问题是当我返回一个简单的字符串时, 比如

def render(self, context):
    return  asd 

但每当我试图返回含有 html 内容的东西时, 它就失败了:

def render(self, context):
    return mark_safe( <ul class="jqueryFileTree" style="display: none;"><li><ITEM</li></ul> )

默默无闻地失败了,没有做任何事情,有什么帮助吗?

< 加固 > EDIT : 加标记_ 安全。 仍然不起作用

<强度 > EDIT :标签 :

    import os
    import urllib

    from django import template
    from django.utils.safestring import mark_safe

    register = template.Library()

    class DirTree(template.Node):
        def __init__(self, start_dir):
            self.start_dir = start_dir
        def render(self, context):
            # CODE THAT GENERATES A HTML NESTED LIST
            return mark_safe( <ul class="jqueryFileTree"><li><ITEM</li></ul> )

    @register.tag
    def print_tree(parser, token):
        try:
            # split_contents() knows not to split quoted strings.
            tag_name, start_dir = token.split_contents()
        except ValueError:
            raise template.TemplateSyntaxError("%r tag requires a single argument" % token.contents.split()[0])
        if not (start_dir[0] == start_dir[-1] and start_dir[0] in ( " , " ")):
            raise template.TemplateSyntaxError("%r tag s argument should be in quotes" % tag_name)
        return DirTree(start_dir[1:-1])



# TEMPLATE.HTML
# HTML  N STUFF
    <div id="file-tree">
        {% print_tree "catflow_portal/static/repo/usr/test-user/catalogs/food/" %}
    </div>
#END TAGS
问题回答

我认为你的问题是 你需要在模板中使用 ...\\\\\ safe 来告诉 Django 以 html 显示此结果, 而不是强制用 显示为字符串... ..."

< a href=>https://docs.djangoproject.com/en/1.4/ref/templatels/buildingins/#secure" rel=“没有跟随 nofollown noreferrerr>>https://docs.djangoproject.com/en/1.4/ref/templates/buildins/#safecure

Update 2021-10-20: Django 3.2
...|safe Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.

If you are chaining filters, a filter applied after safe can make the contents unsafe again. For example, the following code prints the variable as is, unescaped:
{{ var|safe|escape }}

< a href=>https://docs.djangoproject.com/en/3.2/ref/templatels/buildingins/#std:templatefilter-security" rel=“无跟随 noreferrerr'>https://docs.djangoproject.com/en/3.2/ref/templates/buildingins/#std:templutfilter-security





相关问题
Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

How to create a submit button template in Oracle APEX?

I m trying to create a template for a button in Oracle APEX but I don t seem to have access to the appropriate substitution strings to make it work. For non-templated buttons APEX seems to insert a ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Load ruby on rails template from database

I wonder if there is a way for me to store ruby on rails view files into database store and have that fetched directly from there. The reason is I want to create a CMS with all the user data stored in ...

Templated HTML Editor

I m looking for a HTML editor that kinda supports templated editing or live snippets or something like that. Background: I m working on a website for a friend. As there are no specifications what the ...

Dreamweaver changing path to site s reference instead of local

I have noticed recently, when I apply a template to a new HTML website, all the relative paths are pointed to my local files, example: file:///C|/webstuff/files but I cannot set them to relative paths ...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签