[Fixed]-ImageField url returns an empty string

1👍

Update: after seeing the your original files, this looks like a much simpler problem.

On your FeaturedImage model, you have an Image field, not an Imagem field. I don’t know if that was a typo or not, but if that was pasted correctly, that would be the source of an issue.

i.e., you should try:

<div class="banner-bottom" style="background: url( {{ image.Image.url }} )no-repeat center;">

Imagem is a field on your Post model, and that may have been the source of the confusion.


Your view looks like it couldn’t possibly work, so there may be an issue if this is the actual view file:

# views.py
def renderFoo(request):
    # The below would give you a syntax error
    context = {
        'foos' = foo.objects.all(),
        ''' other models here '''
    }
    # You should be using:
    context = {
        'foos': foo.objects.all(),
        # other models here
    }
    return render(request,'path/to/page.html',context)    

And it should work properly. Accessing the url field like you are in the view is the proper way to access the image URL.

>>> car = Car.objects.get(name="57 Chevy")
>>> car.photo
<ImageFieldFile: chevy.jpg>
>>> car.photo.name
'cars/chevy.jpg'
>>> car.photo.path
'/media/cars/chevy.jpg'
>>> car.photo.url
'http://media.example.com/cars/chevy.jpg'
👤2ps

Leave a comment