[Django]-How to keep empty lines in a textarea in Django admin UI?

4👍

You need to set strip=False [Django-doc] to disable stripping leading and trailing space:

def formfield_for_dbfield(self, db_field, **kwargs):
    formfield = super().formfield_for_dbfield(db_field, **kwargs)
    if db_field.name == 'some_text':
        formfield.strip = False
        formfield.widget = forms.Textarea(attrs={'rows': 10, 'cols': 80})
    return formfield

Leave a comment