so I've defined readonly_fields in Django ModelAdmin with a callable which looks like this:
class TestAdmin(admin.ModelAdmin):
readonly_fields = ("test_field_with_whitespace",)
def test_field_with_whitespace(self, obj):
return ' Hello World'
In the corresponding admin view the readonly field is shown as "Hello World" - so without the whitespaces.
What can I do to show the whitespaces?
This is nothing to do with the modeladmin. HTML itself strips all whitespace. If you need it to show precisely then you will need to wrap it in a <pre>
tag - and since you are now sending HTML you need to mark it as safe to avoid autoescaping.
from django.utils.safestring import mark_safe
...
return mark_safe('<pre> Hello World</pre>')