176π
None, False and True
all are available within template tags and filters. None, False
, the empty string ('', "", """"""
) and empty lists/tuples all evaluate to False
when evaluated by if
, so you can easily do
{% if profile.user.first_name == None %}
{% if not profile.user.first_name %}
A hint: @fabiocerqueira is right, leave logic to models, limit templates to be the only presentation layer and calculate stuff like that in you model. An example:
# someapp/models.py
class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
# other fields
def get_full_name(self):
if not self.user.first_name:
return
return ' '.join([self.user.first_name, self.user.last_name])
# template
{{ user.get_profile.get_full_name }}
101π
You can also use another built-in template default_if_none
{{ profile.user.first_name|default_if_none:"--" }}
- [Django]-Alowing 'fuzzy' translations in django pages?
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-Add a custom button to a Django application's admin page
17π
You can also use the built-in template filter default
:
If value evaluates to False (e.g. None, an empty string, 0, False); the default βββ is displayed.
{{ profile.user.first_name|default:"--" }}
Documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#default
- [Django]-Django development IDE
- [Django]-How to change field name in Django REST Framework
- [Django]-Removing 'Sites' from Django admin page
16π
is
operator : New in Django 1.10
{% if somevar is None %}
This appears if somevar is None, or if somevar is not found in the context.
{% endif %}
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-How to recursively query in django efficiently?
- [Django]-Add a custom button to a Django application's admin page
6π
- [Django]-Django: Example of generic relations using the contenttypes framework?
- [Django]-How to use subquery in django?
- [Django]-Django REST Framework β 405 METHOD NOT ALLOWED using SimpleRouter
4π
{% if profile.user.first_name %}
works (assuming you also donβt want to accept ''
).
if
in Python in general treats None
, False
, ''
, []
, {}
, β¦ all as false.
- [Django]-POST jQuery array to Django
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
3π
In cases where we need to validate for a field with null
value, we may check so and process as under:
{% for field in form.visible_fields %}
{% if field.name == 'some_field' %}
{% if field.value is Null %}
{{ 'No data found' }}
{% else %}
{{ field }}
{% endif %}
{% endif %}
{% endfor %}
- [Django]-Using django-rest-interface
- [Django]-Programmatically saving image to Django ImageField
- [Django]-What is a "slug" in Django?
1π
You could try this:
{% if not profile.user.first_name.value %}
<p> -- </p>
{% else %}
{{ profile.user.first_name }} {{ profile.user.last_name }}
{% endif %}
This way, youβre essentially checking to see if the form field first_name
has any value associated with it. See {{ field.value }}
in Looping over the formβs fields in Django Documentation.
Iβm using Django 3.0.
- [Django]-Django-Forms with json fields
- [Django]-Django REST Framework : "This field is required." with required=False and unique_together
- [Django]-Django: Why do some model fields clash with each other?
1π
Just a note about previous answers: Everything is correct if we want to display a
string, but pay attention if you want to display numbers.
In particular when you have a 0 value bool(0)
evaluates to False
and so it will not display and probably is not what you want.
In this case better use
{% if profile.user.credit != None %}
- [Django]-How to check if ManyToMany field is not empty?
- [Django]-How to limit the maximum value of a numeric field in a Django model?
- [Django]-How to test auto_now_add in django