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…
142
Use the built-in default_if_none
filter.
{{ client.user|default_if_none:" " }}
{{ client.user|default_if_none:"" }}
- [Django]-Django – Overriding the Model.create() method?
- [Django]-Django testing: Test the initial value of a form field
- [Django]-How can I pass my context variables to a javascript file in Django?
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.
- [Django]-What is the difference between null=True and blank=True in Django?
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Remove pk field from django serialized objects
Source:stackexchange.com