Django template filter - one line
I'm looking for a Django template filter that turns a multi-line construction into one big line. Has anyone implemented it? The reason is - I have a form, {{form.as_p}} creates a multi-line html开发者_运维技巧 fragment, I want to create a javascript variable which is an html fragment, but when I do like this:
var new_div_text = '{{form.as_p}}';
it doesn't work. The reason is obvious, in javascript constructions like
var hello = 'Hello
world';
are invalid!
Reading your use case, it doesn't appear that you just want to remove lines. What if one of your form labels contains a ' character? Oops, your javascript is now invalid. Django comes with a filter called escapejs which us used for precisely this problem.
With escapejs, you would type:
var newDivText = '{{ form.as_p|escapejs }}'
and you won't have to worry about any characters destroying your javascript.
精彩评论