Yura Liashenko
Yura Liashenko

Reputation: 57

How to show code snippet in django template, "|safe" filter not helps

I'm trying to make app like gist.github.com. After saving code snippets they looks in like long string. I tried different filters like "safe, escape ... etc". Nothing helped me.

In database code looks like:

def asd(a):
    return a+2                                                     
asd(2)

This is my template code:

<div>{{ s.code|escape }}</div>

Result is:

def asd(a): return a+2 asd(2)

Upvotes: 2

Views: 334

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

This doesn't have anything to do with escaping or marking as safe. In fact, it doesn't have anything to do with Django at all.

HTML ignores whitespace, including newlines. If you want to show your code as formatted in the db you should use the <pre> tag.

Upvotes: 3

Related Questions