[Django]-Django: Want to display an empty field as blank rather displaying None

8👍

you may use:

{% if client %} {{client.user}} {% else %}   {% endif %}

Checking with an if is enough, so you may not user else block if you want…

👤Mp0int

142👍

Use the built-in default_if_none filter.

{{ client.user|default_if_none:" " }}
{{ client.user|default_if_none:"" }}

7👍

this is such a strange problem.
I have a good idea for it. If you want to modify your field at display time than rather checking it at template , check it at your model class.

ExampleModel(models.Model):
    myfield = models.CharField(blank=True, null = True)

    @property
    def get_myfield(self)
        if self.myfield:
              return self.myfield
        else:
              return ""

Use it in your template directly instead of field.

 {{ExampleModel.get_myfield}}

you never need to change your template to change this field in future, just modify you property.

Leave a comment