user10421193
user10421193

Reputation: 217

Source specified in Javascript not showing up in Django template

Maybe this is a really dumb question, but I'm learning Javascript and Django and can't figure it out. I've spent a few hours on this and maybe it's a syntax error or something else.

I want to specify the source of an image in javascript and then display the image in Django HTML template. It's not working, even with simple images

Here is my javascript:

$(document).ready(function() {
        var image = document.getElementById("image").src('https://res.cloudinary.com/.../{{ object.id}}');
        }

Here is my image info:

<img src="#" id="image" class="mx-auto" width="250px" onerror='this.onerror = null; this.src="https://res.cloudinary.com/.../image.jpg"'/>

What am I doing wrong? It's definitely not working - the image is not showing up even when it exists. I either get the default image or no image.

The javascript is in the HTML file and this is what I have at the top of my file:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Upvotes: 1

Views: 169

Answers (1)

getup8
getup8

Reputation: 8228

Try changing your js:


document.getElementById('image').src = 'https://res.cloudinary.com/.../{{ object.id}}';

Upvotes: 2

Related Questions