Search code examples
djangodjango-template-filters

Limiting number of <br> tags generated via Django's linbreaksbr filter


For a user-generated content forum built via Django, I'm contemplating using the filter linkbreaksbr when displaying content in the template. One problem is that it converts all user-entered new lines into <br> tags. That opens it to abuse (e.g. posts where the submitter enters numerous new lines between sentences).

Whenever there are multiple new line characters, I'd prefer the result to be a single <br>.

Is there any way to achieve this functionality via Django's linebreaksbr? If not, what would be a way to override this and create my required functionality? It would be nice to get an illustrative answer.


Solution

  • I suggest you that reduce number of new lines at save time, so in MyModel.save or in your form validation simply replace multiple new lines with one

    also check this question