1

I'm working on a d3 based visualization tool that allows users to click paths on images. That works all fine until I tried to let the user change the background image by a simple dropdown.

The problem was that javascript can not load files from the disk since to Cross-Origin permissions. So the solution I picked was to set up a backend. I choose Django.

This is the javascript code which manipulates the image:

window.LOADED_FILE = "PUT PATH HERE";

I have a working dropdown where the user can pick an image and I get then the path to the file. After this, I tried to load the image like the following.

window.SVG.append("image")
        .attr("width", window.WIDTH + "px")
        .attr("height", window.HEIGHT + "px")
        .attr("xlink:href", window.LOADED_FILE);     

This doesn't work at all and I only get a placeholder image for the 404 Request. So tried to following two things:

  1. First of all, this is a solution that I found here. This works fine, but I don't want and I cant hardcode every possible image into an HTML file just to pick the correct variable when the user wants to select another image them later when the user wants. So I need a more dynamic solution.

  2. After this, I tried the following. But when I do console.log(DJANGO_STATIC_URL) I get a an empty log. In the settings.py file contains the following static url: STATIC_URL = '/static/'.

So what I want is a solution on how I should load images with javascript without hardcoding them in before in an HTML file. I suspect that I have misunderstood something or ignored something obvious.

5
  • Let me get this straight, so you want to be able to load all the images from the 'static' folder in the page, using JavaScript? Commented Oct 7, 2019 at 15:42
  • {{ STATIC_URL }} is only in the context if you have django.template.context_processors.static in your TEMPLATES context_processors options. Commented Oct 7, 2019 at 16:01
  • @CatalinHoha yes exactly, but I don't want to have in my HTML file all images as variables. There are other parts of the workflow which generates the images and put them in the static folder. Commented Oct 8, 2019 at 8:50
  • @dirkgroten I'll see if that helps me thank you. Commented Oct 8, 2019 at 8:56
  • @dirkgroten It worked, with the flag I can access my static folder and build the complete path. Thanks a lot. Commented Oct 8, 2019 at 9:07

1 Answer 1

1

{{ STATIC_URL }} is only in the context if you have "django.template.context_processors.static" in your TEMPLATES context_processors options in settings.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.