Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django';s render_to_字符串生成<;br>;标签_Python_Django_Python 2.7 - Fatal编程技术网

Python Django';s render_to_字符串生成<;br>;标签

Python Django';s render_to_字符串生成<;br>;标签,python,django,python-2.7,Python,Django,Python 2.7,Django 1.10、Python 2.7 我正在使用render_to_string函数获取模板的渲染内容,并在管理员自定义字段的内容中使用它。 因此,我在Admin类中有类似的代码: def checks(self, obj): # ... return render_to_string('template.html', context) checks.allow_tags = True 在template.html中,我只有一个表: <

Django 1.10、Python 2.7

我正在使用
render_to_string
函数获取模板的渲染内容,并在管理员自定义字段的内容中使用它。 因此,我在Admin类中有类似的代码:

    def checks(self, obj):
       # ...
       return render_to_string('template.html', context)

    checks.allow_tags = True
template.html
中,我只有一个表:

<table>
    {% for something in somethings %}
      <tr><td>...</td></tr>
    {% endfor %}
</table>

{somethings%中的某物为%s}
...
{%endfor%}
但这会在实际表格之前产生大量的

s:

<br>
<br>
<br>
...
<br>
# Actual table
<br>
<br>



...
#实际表格

这个案子有什么问题


UPD。我可以通过删除模板文件中的空格和换行符来“修复”。但这显然不是我想要的工作方式

允许标记是:

在旧版本中,可以向方法添加allow_tags属性 防止自动逃逸。此属性不推荐使用,因为它更安全 改为使用format_html()、format_html_join()或mark_safe()

尝试:


allow_标签
是:

在旧版本中,可以向方法添加allow_tags属性 防止自动逃逸。此属性不推荐使用,因为它更安全 改为使用format_html()、format_html_join()或mark_safe()

尝试:


您的
template.html
标记前后是否有空格?@evansumrithi-nope。它以
开头,以
结尾。您的
模板.html
标记前后是否有空格?@Evasmurithi nope。从django的src:
类AdminReadonlyField(对象):。。。def内容(自身):。。。如果hasattr(value,“\uuuhtml\uuuuu”):result\u repr=value。。。其他:。。。result\u repr=linebreaksbr(result\u repr)
。这意味着如果您返回mark_safe(),它将不会调用
linebreaksbr
,您也不会看到

s。请重试。从django的src:
类AdminReadonlyField(对象):。。。def内容(自身):。。。如果hasattr(value,“\uuuhtml\uuuuu”):result\u repr=value。。。其他:。。。result\u repr=linebreaksbr(result\u repr)
。这意味着如果您返回mark_safe(),它将不会调用
linebreaksbr
,您也不会看到

s。请再试一次。
def checks(self, obj):
       # ...
       return mark_safe(render_to_string('template.html', context))