[Answered ]-Django Boolean Field returns False in template while it actually is True

2👍

✅

These lines in your view and template do not match.

sherlock = Sherlock.objects.filter(owner=self.pk)

{% if sherlock.wifi %}

You can’t access the wifi attribute on the queryset, you access it on the instance.

If the queryset might contain multiple items, then you can loop through the instances in the template.

{% for s in sherlock %}
    {% if s.wifi %}
    Wifi
    {% endif %}
{% endfor %}

If the queryset should only ever return a single instance, then you can use get() instead of filter().

sherlock = Sherlock.objects.get(owner=self.pk)

You can improve this to handle the case where the object does not exist

try:
    sherlock = Sherlock.objects.get(owner=self.pk)
except Sherlock.DoesNotExist:
    # do something here

Sometimes the get_object_or_404 shortcut is useful for this.

sherlock = get_object_or_404(Sherlock, owner=self.pk)

Finally, note that there is no need to compare to True in the template, just use

{% if sherlock.wifi %}

Leave a comment