django渲染从views.py传递的html标签(django render html tags passed from views.py)

编程入门 行业动态 更新时间:2024-10-28 20:17:55
django渲染从views.py传递的html标签(django render html tags passed from views.py)

我希望我的用户能够编写,例如image1并使用正则表达式识别并用标签替换它。 我把它全部工作 - 但是当我将数据传递给模板进行显示时,我这样做:

<div class="container"> <p> {% articleTxt %} </p> </div>

ArticleTxt包含:

This is a test Article <img src="/media/articleImages/django-allauth.png"> and that was a picture and this is another one then some more text ---- original: This is a test Article *image1* and that was a picture and this is another one *image2* then some more text

多数民众赞成我的沮丧。 我已经尝试将模板更改为:

{% autoescape off %} {{articleTXT}} {{% autoescape %}}

我也试过了

{{% articleTXT|safe %}}

它总是打印标签而不是渲染它们。 反正是否有渲染标签嵌入在从views.py传递到我的模板的文本中!!

I want my user to be able to write, for example image1 and use a regex to recognize and replace that with an tag. I have it all working -- but when I pass the data to template for display I do so like this:

<div class="container"> <p> {% articleTxt %} </p> </div>

ArticleTxt contains:

This is a test Article <img src="/media/articleImages/django-allauth.png"> and that was a picture and this is another one then some more text ---- original: This is a test Article *image1* and that was a picture and this is another one *image2* then some more text

Thats my frustration. I've tried changing the template to:

{% autoescape off %} {{articleTXT}} {{% autoescape %}}

I also tried

{{% articleTXT|safe %}}

It always prints the tags instead of rendering them. Is there anyway to render tags embedded in text passed from views.py to my template!!

最满意答案

{{ articleTXT |safe }}

要么

{% autoescape off %} {{ articleTXT }} {% endautoescape %}

应该做的伎俩

*注意:它的{%endautoescape%}

如果那不工作尝试,

format_html(articleTXT)

在你的意见。

希望这可以帮助 :)

{{ articleTXT |safe }}

or

{% autoescape off %} {{ articleTXT }} {% endautoescape %}

should do the trick

*Note: its {% endautoescape %}

if that isnt working try,

format_html(articleTXT)

in your views.

Hope this helps :)

更多推荐

本文发布于:2023-08-08 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   py   django   views   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!