[Fixed]-AttributeError at:'NoneType' object has no attribute 'src'

1đź‘Ť

Consider what Python is telling you about the error. Your article object simply doesn’t have a top_image. No top_image, no src attribute. Make sure your article has an image, which probably means uploading something since you are calling it “media”.

This isn’t a programming error. Though you should consider adding some error checking for this type of situation. Unless the Article object requires a top_image and can’t be saved/instantiated without one, it’s likely someone will program an Article without an image and you’ll see this error again someday. Also, you assigned the resposne (typo?) variable and never use it.

Here’s how you can replicate your error, in a shell, no Django required:

>>> top_image = None
>>> top_image.src
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'src'
👤JCotton

Leave a comment