LoginSignup
5
4

More than 3 years have passed since last update.

Djangoの {{ 〇〇 |safe }}とは

Posted at

|safeはHTMLタグを描き出せるようにするためのフィルター機能。

通常、{{ 〇〇 }}の中にHTMLタグを埋め込んでもただのテキストとして表示されてしまう。
そこで、タグとして表示させたい時には{{ 〇〇 |safe }}とする。

例として、<h1>hello</h1>を表示させてみる

index.html
<body class="container">
    {{title}}
    {{title|safe}}
</body>
views.py
def index(request):
    params = {
        'title':'<h1>hello</h1>'
    }
    return render(request, 'hello/index.html', params)

次のようになる。

image.png

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4