[Answered ]-ValueError: The 'Image' attribute has no file associated with it

1👍

The error causes because no image is associated with the image field. Use the following code to store image properly:

def bookdata(req):
if req.method == "POST":
    b_name = req.POST.get('book name')
    a_name = req.POST.get('author name')
    p_year = req.POST.get('published year')
    price = req.POST.get('price')
    image = req.FILES['image']    # Change Done Here
    obj = BookDetails(Name=b_name, A_Name=a_name, P_Year=p_year, Price=price, Image=image)
    obj.save()
    return redirect(add_books)

In the template, you can also make some changes like :

<td>
    {% if i.Image %}
         <img src="{{ i.Image.url}} ">
    {% else %}
         <img src="#path_to_default_image">   
    {% endif %}
</td>

Leave a comment