【问题标题】:how can i get rid of space and amp inside of django template variable without using js or html如何在不使用 js 或 html 的情况下摆脱 django 模板变量中的空间和 amp
【发布时间】:2019-09-23 02:26:35
【问题描述】:

我不确定如何删除 django 模板变量中的空格和 &amp。实际上,我在加载页面时将 url 字符串呈现为 django 变量。

但它会自动包含 &amp 和空格。 毕竟,在 html 视图中,当我在 js 控制台中打印出来时,它会在变量内部显示 &amp, %。

当然,我可以通过替换它们来修复 js 代码,但是代码会变得混乱 所以我想用另一种方式使用 python 或 django 代码。

有什么想法可以在服务器端删除它们吗?

结果:

http://127.0.0.1:8000/users?key=fff&ids=(1%,3%,4%,12%)

我的期望:

http://127.0.0.1:8000/users?key=fff&ids=(1,3,4,12)

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    假设在您的上下文中有

    context['url'] = 'http://127.0.0.1:8000/users?key=fff&ids=(1,3,4,12)'
    

    如果您想按原样获取内容,不进行任何转义,可以使用safe template tag

    {{ url | safe }}
    

    【讨论】:

    • 非常感谢。伊兰你是 django 专家!太棒了!
    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多