簡體   English   中英

如何將HTML字符串發送到Django模板JavaScript?

[英]How to send the html string to django template javascript?

這是我從django視圖發送到django模板的html字符串的內容,

def myview(request):
   return render(request, 'mypage.html',{'html': """" <p>sdgsajdgjs</p>

<pre><code>fafkjhfd
</code></pre> """})

的mypage.html:

 <script>
    var html = "{{ html }}"
</script> 

但是我Uncaught SyntaxError: Unexpected token ILLEGAL這個錯誤: Uncaught SyntaxError: Unexpected token ILLEGAL 我知道此錯誤,因為所有HTML代碼均未視為字符串。 請給我答復如何解決此錯誤。

使用escapejs過濾器。 嘗試這個,

<script>
    {% autoescape off %}
    var html = "{{ html|escapejs }}"
    {% endautoescape %}
</script>

更新:

{% autoescape off %}
    {% filter escapejs %}
       {% include "....html" with x=y %}
    {% endfilter %}
{% endautoescape %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM