呈现的脚本显示在主体模板 django 中

rendered script display inside body template django

抱歉我的英语不好。 我用

{% ishout.js %}

head 中以在 django 模板中呈现脚本文件,但它们显示为字符串

"<script type="text/javascript" src="http://localhost:5500/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://localhost:5500/client/ishout.client.js"></script>"

并且在 body 内,而不是在 head 内。 之后的其他脚本也显示在正文中。

我搜索并通过编码找到了答案,但找不到解决方法。

更新 这是views.py个文件

@login_required
def home(request):
    users = User.objects.exclude(id=request.user.id)
    v = RequestContext(request, {'users':users})
    # return render(request, 'home.html', {'users':users})
    return render_to_response('home.html', v)

这是home.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Home</title>
    {% load drealtimetags %}
    {% ishout_js %}
</head>
<body>
    <h1>Dashboard</h1>
    {% for user in users %}
        {{ user.first_name }}<a href="/alert/?user={{ user.id }}">Alert (Hello)</a>
    {% empty %}
        <b>No user found</b>
    {% endfor %}
</body>
</html>

更新 2: 这是页面来源

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Home</title>


    &lt;script type=&quot;text/javascript&quot; src=&quot;http://localhost:5500/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;http://localhost:5500/client/ishout.client.js&quot;&gt;&lt;/script&gt;

    <script>
        ishout.on('alertchannel', function(data){
            alert(data.msg);
        });

        ishout.init();
    </script>
</head>
<body>
    <h1>Dashboard</h1>

        phuc<a href="/alert/?user=2">Alert (Hello)</a>

        <a href="/alert/?user=3">Alert (Hello)</a>

</body>
</html>

当我检查时

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>


</head>
<body>
&lt;script type="text/javascript" src="http://localhost:5500/socket.io/socket.io.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://localhost:5500/client/ishout.client.js"&gt;&lt;/script&gt;

    <script>
        ishout.on('alertchannel', function(data){
            alert(data.msg);
        });

        ishout.init();
    </script>


    <h1>Dashboard</h1>

        phuc<a href="/alert/?user=2">Alert (Hello)</a>

        <a href="/alert/?user=3">Alert (Hello)</a>


</body>

Snapshot

由于您包含了每个 js 文件,并且您希望将其呈现和解释为 html 脚本标签,请尝试将其设为模板文件 _ishout_includes.html。您根本不需要更改文件的内容。

从那里,使用 {%include%} 标签。看起来像

{% include _ishout_includes.html%}

这是因为标签返回的 HTML 被转义了,所以浏览器将其呈现为正文中的文本,而不是 <head> 中的真实 HTML。

关闭标签周围的自动转义:

{% autoescape off %}
    {% ishout_js %}
{% endautoescape %}