如何防止django从表单文本字段输入中删除换行符?
当我在表单的文本字段中输入它时:
AAA
BBB
CCC
我希望它保持如上所述。但相反,django将其转换为:
aaa bbb ccc
我试过像这样覆盖clean()方法(我希望删除多个空格,但不要删除EOL字符):
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 = ['']
但它不起作用。有任何想法吗? 感谢
解决方案:
{{ value|linebreaksbr }}