问题描述
我想要的就像堆栈溢出。用户可以HTML格式化他们的文本输入,页面应该以相同的方式呈现,我使用 wmd.js
存储格式化的输入,考虑我有一个上下文变量 {{variable}}
与字符串值< p> / p>中
。当我渲染模板时,
{{variable}}输出< p> something< / p>
和{{variable | safe}}也输出< p> something< / p>
它将HTML标签显示为页面中的文本。如何在 {{variable}}
中呈现HTML标签,但不将其显示为纯文本。
模板
< div id ='thread_answer_content'>
{%for question.answer_set.all%}
{{answer.answerbody | safe}}
{%endfor%}
< / div>
视图
code> def detail(request,question_id):
q = get_object_or_404(Question,pk = question_id)
return render_to_response('CODE / detail.html',{'question':q},
context_instance = RequestContext(request)
)
这里是django管理页面这个问题,我使用sqlite3的方式
使用标签:
{%autoescape off%} {{variable}} {%endautoescape%}
What I want is like stack overflow. User can HTML format their text input, and the page should be rendered exactly in the same way,
I use the wmd.js
to store the formatted input, Consider I have a context variable {{variable}}
with string value "<p>something</p>"
. When I render the template,
{{variable}} outputs <p>something</p>
and {{variable|safe}} also output <p>something</p>
It shows the html tag as text in the page. How to render the HTML tag in the {{variable}}
but not showing them as plain text.
the template
<div id='thread_answer_content' >
{% for answer in question.answer_set.all %}
{{answer.answerbody|safe}}
{% endfor %}
</div>
the view
def detail(request,question_id):
q = get_object_or_404(Question,pk=question_id)
return render_to_response('CODE/detail.html',{'question':q},
context_instance = RequestContext(request)
)
here is the django admin page of the question , am using sqlite3 by the way
use tag : http://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape
{% autoescape off %}{{ variable }}{% endautoescape %}
这篇关于如何将django模板变量呈现为html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!