Skip to content Skip to sidebar Skip to footer

Try And Except Method To Disable Error Messages

I'm getting AttributeError at /add_post/'NoneType' object has no attribute 'src' whenever I don't provide url which is not a required field. I just want to use try and catch method

Solution 1:

Simply check whether top_image is none.

if article is not None:
    if article.top_image is not None:
        return {'image':article.top_image.src}

return {'image':'default image'}

Solution 2:

If that's the only error then:

try:
    resposne = {'image':article.top_image.src}
except AttributeError:
    resposne = some_image_object

Post a Comment for "Try And Except Method To Disable Error Messages"