如何将html代码放入Django模板中

问题描述:

尝试从另一个html文件发送到模板html代码。 视图代码看起来像:如何将html代码放入Django模板中

def homepage(request): 

    return render(request, 'homepage.html', { 
     'table': generateTable(), 
    }) 

def generateTable(): 
    x = render_to_response('tableSchema.html') 
    return x 

而且在模板:

{{ table|safe }} 

一切都是好的,但我看到UTF8表上面的信息: 我的意思是该表是否正确产生,但以上是以下文本:

Content-Type:text/html; charset = utf-8

你知道为什么以及如何删除它吗? 由于提前,

render_to_response不正是顾名思义:渲染模板和创建响应。你不需要回应,你只需要渲染的字符串:所以使用render_to_string

但是,更自然的做法是使用模板标签或包含在模板本身内。

+0

它的工作原理,感谢您的快速帮助:-) –