[Answer]-Local variable 'like' referenced before assignment

1👍

If you get a Like.DoesNotExist exception, you just pass, without assigning anything to like. But then you try to use it anyway. Hence the error telling you that you tried to use it before assigning anything to it.

If you want to assign some “fallback” value, like None, you can do that explicitly:

try: 
    like = Like.objects.get(pk=obj.id)
except Like.DoesNotExist:
    like = None

Leave a comment