How to prevent django forms removing newline characters

How to prevent django removing the newline characters from the form text field input?

When I type this into the text field in my form:

aaa

bbb

ccc

I want it it to stay like above. But instead django converts it into:

aaa bbb ccc

I've tried overrding clean() method like this (I want multiple spaces to be removed, but not EOL characters):

class ThreadForm(ModelForm):
    def clean(self):
        cleaned_data = self.cleaned_data
        body = cleaned_data.get('body')
        body = re.sub(' +', '', body)
        cleaned_data['body'] = body

        return cleaned_data

    body = forms.CharField(max_length=25000, widget=forms.Textarea, required=False)
    class Meta:
        model = Thread
        fields = ['title', 'posted_by']
        exclude = ['']

but it didn't work. any ideas? Thanks

SOLUTION:

{{ value|linebreaksbr }}

Upvotes: 1

Views: 1677

Answers (1)

doniyor
doniyor

Reputation: 37904

you just need linebreaksbr

{{ value|linebreaksbr }}

Upvotes: 2

Related Questions