0

I have a simple use case in which I need to dynamically add rows to a table in my HTML template. I am using the following javascript code to add this on a button click:

function add_row(){
   row_html = '<tr><td><img src="path/to/my/file.png"></td></tr>'
   $('#table-id').append(row_html);
}

The only problem is that the image is not being found. I have also tried using Django template language, doesn't work. One solution is defining the src in the script tag in my HTML template like in this question. However I am not sure this is the optimal approach.

What is the correct approach to solve this problem?

edferda
  • 440
  • 1
  • 5
  • 11
  • Have a look at the _last_ answer i.e. the [one by @tri.akki7](https://stackoverflow.com/a/67727205/14991864) on your linked question. I find it to be the best out of all those answers (It doesn't render from Django into JavaScript), unfortunately it has the least upvotes :(. I do believe you will find it suitable after perhaps some minor adjustments? :) (the `input` tag can really be any other tag with a custom data-attribute) – Abdul Aziz Barkat Jun 23 '21 at 16:56
  • What is the image path you are using? Do you have an example for that? – Abdullah Khan Jun 23 '21 at 17:05
  • The image is in the app static files folder. I have tried using static/app/img/image.png and also app/img/image.png and also app/static/app/img/image.png . I am following Django name spacing conventions. – edferda Jun 23 '21 at 17:09

0 Answers0