【问题标题】:Django template render html links does not worksDjango模板渲染html链接不起作用
【发布时间】:2019-06-04 00:37:50
【问题描述】:

在我的 django 项目中,我必须将 var ina 模板渲染为 html。 我认为这样做:

con_stat = "<div id='overlay_demo' style='display:block'><div id='text-demo'><div class='login-box-body'><p class='login-box-msg'><strong><font color='red'>MY TITLE HERE</font></strong></p><br>My text here<br><br><div><form><button onclick='location.href=https://mywebsite.com/contact;' class='btn btn-block btn-danger btn-lg'>REPORT THE PROBLEM</button><br><a href='/register/retry'><button class='btn btn-block btn-success btn-lg'>RETRY THE REGISTRATION PROCES</button></a></form></div></div></div></div>"
context_dict = {'all_case': test_case, 'all_set': sg, 'the_stat': con_stat}
response = render(request, b_temp, context_dict, context)

好吧,现在在我的 html 模板中:

{% autoescape off %}{{ the_stat }}{% endautoescape %}

或者我也试试:

{{ the_stat|safe }}

模板现在可以正确显示 html,但问题是链接,我的第一个按钮(带有 onclick= 功能)和第二个按钮(带有 href 链接)都不起作用。 在每种情况下,当我单击时,行为都是重新加载同一页面。

有人遇到过一些与链接有关的问题,像上面那样在 django 模板渲染中调用 javascript?

提前非常感谢

【问题讨论】:

    标签: javascript django python-3.x django-templates html-rendering


    【解决方案1】:

    试试这个

    onclick="javascript:location.href='https://mywebsite.com/contact'"
    

    【讨论】:

    • 谢谢,但我不能使用 "" 和 ' 因为我将变量定义为字符串 " 并且在里面我只能选择使用 '。
    • 我认为必须有一种方法可以像onclick=\"javascript:location.href或其他方式一样逃避双引号
    • javascript:location.href 不起作用,我认为问题可能出在 |safe 或使用 javacall 或链接自动转义
    猜你喜欢
    • 2014-10-05
    • 2016-07-02
    • 1970-01-01
    • 2017-02-11
    • 2013-06-27
    • 2017-06-04
    • 2022-07-22
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多