Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

django - template filter to trim any leading or trailing whitespace

Is there a template filter in django that will trim any leading or trailing whitespace from the input text.

Something like: {{ var.example|trim }}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Django templates allow you to access methods and properties by using the '.' syntax:

{{ var.example.strip }}

You can extend this by chaining other filters when you're dealing with HTML, e.g.:

{{ var.example.strip|safe|removetags:"p img" }}

Here we first remove any <p> and <img> tags, then tell Django it can safely render the rest of the content, which we have stripped of any whitespace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...