【问题标题】:Django render_to_string <body><head> tagDjango render_to_string <body><head> 标签
【发布时间】:2016-05-06 23:13:16
【问题描述】:

我是我的 django 应用程序,我想将静态 html 页面加载到主模板中。这个静态 html 页面是一个电子邮件模板。我的目标是在我的主模板中编辑这封电子邮件。电子邮件 html 页面在 url.py 中没有视图。我不在主模板中使用include,也不想使用iframe。问题是我想加载所有的 html 电子邮件标签(比如

 <html>
 <head>
 <body>

) 但是当我进行页面渲染时,这个标签会被删除(或者我在主模板中看不到它们......)。这是我的代码:

view.py

def my_view(request):
    file_name = "/templates/path/emailtemplate.html"
htmlblock = render_to_string(file_name, {})
return render_to_response('main_template.html', {
        'htmlblock': htmlblock,
    },
    context_instance = RequestContext(request))

ma​​in_template.html

    <html>

<head>
  ....
  <head>
    <body>
      <div id="content">
        {{htmlblock}}
      </div>
    </body>
</html>

这是我想要的:

<html>

<head>
  ....
  <head>
    <body>
      <div id="content">
        <html>
        <head>
          ....
        </head>

            <body>
              ...
            </body>
        </html>
      </div>
    </body>
</html>

如果没有 iframe,这可能吗?非常感谢您的帮助。

编辑

我的目标是拥有

 <html>
 <head>
 <body> 

标记成

 <div id="content"> 

我加载电子邮件模板的位置。

【问题讨论】:

  • 不清楚你在问什么,你的“视图”中也缺少一个结束引号
  • 谢谢@Sayse。我已经更新了我的描述。我希望现在更清楚

标签: python html django


【解决方案1】:

My goal is to edit this email in my main template.

您可以通过

将模板读入字符串
with open('/templates/path/emailtemplate.html', 'r') as content_file:
    content = content_file.read()

您可以将相同的值用作编辑字段的值,它将像往常一样可见。这可以用作。

return render_to_response('main_template.html', {
        'htmlblock': content,
    }, context_instance = RequestContext(request))

您可以在编辑字段中进一步使用相同的:

<textarea value={{htmlblock}}>

或者在

中正常渲染
<div id="content">{{htmlblock}}</div>

【讨论】:

  • 感谢您的回复。我不知道这是否有效。如果我在主模板中得到像字符串一样的“emailtemplate.html”,我会看到代码而不是呈现的电子邮件。
  • 哦,当您说编辑时,我感到困惑。如果你想渲染它,没有主流方法可以在 div 中使用 标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
  • 2013-01-27
相关资源
最近更新 更多