django views if statement not working with a boolean django views if statement not working with a boolean django django

django views if statement not working with a boolean


Your print statement shows True or False because you are returning the string representation of a boolean value in your str override. In other words, you are printing the strings 'True' or 'False'. The actual boolean field confirmed is a field in your model. You should change your if condition to:

if not confirmed.confirmed:    ...

By the way, it may be a better idea to use get_object_or_404 method instead of get() to return a 404 page instead of a server error when no EmailConfirmed objects could be found:

from django.shortcuts import get_object_or_404...def awaiting_email_confirmation(request):    confirmed = get_object_or_404(EmailConfirmed, user=request.user)    if not confirmed.confirmed:        ...


I adapted the code from catavaran & Selcuk.

view.py:

from django.shortcuts import get_object_or_404def awaiting_email_confirmation(request):confirmed = get_object_or_404(EmailConfirmed, user=request.user)if not confirmed.confirmed:    template = 'accounts/email_confirmation.html'    context = {}    return render(request, template, context)else:    return HttpResponseRedirect(reverse("dashboard"))

This is now working with my test cases.