简体   繁体   English

使用Javascript和Django模板动态加载HTML图像

[英]Dynamic HTML image loading using Javascript and Django templates

I am trying to load a specific image using javascript in a django template, but seem to be running into an issue based on the way django tags are formatted. 我正在尝试在django模板中使用javascript加载特定的图像,但是基于django标签的格式化方式,似乎遇到了问题。

I have a standard django static asset source in an img tag like below: 我在img标签中有一个标准的django静态资产来源,如下所示:

{% load static %}
<img src="{% static 'img/folder/logo1.svg' %}" id="orgLogo" alt="Logo" height="100px"/>

I am displaying data from django lower on the page like so: 我在页面下方显示来自django数据,如下所示:

<a id="orgName" class="side_nav_item">{{ thing.org_name }}</a>

What I would like to do is have the img src change based on the value of thing.org inside the django tags. 我想做的是根据django标签内的thing.org的值对img src更改。

I originally thought jquery would be the best option, but it doesn't seem to play nice with django tags: 我本来以为jquery是最好的选择,但它与django标签的搭配似乎不太好:

$(document).on('load', '#orgName', function(){
    $('img').attr('src','{% static 'img/folder/''+$('#orgLogo').val()+' %}');
});​​​​​​

I get error because of the single quotes required as part of the django syntax. 由于django语法的一部分需要单引号,因此出现错误。

I after I was able to get the basics of this javascript function to work, I was planning on using a javascript switch statement to test for specific values and assign an image (ex logo1.svg, logo2.svg, etc.) to each value (ex org1, org2, org2) thing.org can load (it's a pre-defined set of values that's not long and not likely to change). 在能够使用此javascript函数的基础知识之后,我计划使用javascript switch语句测试特定值并为每个值分配一个图像(例如logo1.svg,logo2.svg等)。 (例如org1,org2,org2) thing.org可以加载(这是一组预定义的值,长度不长且不太可能更改)。

Any suggestions on how to approach this? 关于如何处理此问题的任何建议?

You can't combine stuff that is evaluated on the back-end, on your server (template tags) and stuff that's evaluated on the front-end, inside the end-user's browser (javascript): Your {% static %} template tag needs to be evaluated by Django before an HTML document is sent to the browser. 在最终用户的浏览器(javascript)中,您无法将在后端,在服务器上评估的内容(模板标记)和在前端,评估的内容组合在一起:您的{% static %}模板标记将HTML文档发送到浏览器之前 ,需要由Django评估。 You're trying to mix javascript evaluated variables into the {% static %} tag but that makes no sense since they get evaluated at completely different points in time (and on different computers). 您正在尝试将JavaScript评估的变量混入{% static %}标记中,但这没有意义,因为它们是在完全不同的时间点(以及在不同的计算机上)进行评估的。

You can use template tags to create strings of course and then use that later in javascript: 您可以使用模板标记创建字符串,然后在javascript中稍后使用它:

var baseURL = "{% static 'img/folder/' %}"
$(document).on('load', '#orgName', function(){
    $('img').attr('src', baseURL + $('#orgLogo').val());
});​​​​​​

If you look at the HTML source file in your browser developer tools you'll see this: 如果在浏览器开发人员工具中查看HTML源文件,则会看到以下内容:

var baseURL = "https://mycdn.net/static/img/folder/"  // or whatever your STATIC_URL is

I was able to solve this without using javascript . 我无需使用javascript就可以解决此问题。

Here is the working code: 这是工作代码:

{% load staticfiles %}
<img src="{% with 'img/folder'|add:thing.orgName|add:'.svg' as image_static %}
   {% static image_static %}
   {% endwith %}
   {% load static %}" id="orgLogo" alt="Logo" height="100px"
/>

The image file has to be the same name as the value of thing.orgName , but for my use case, that's not a problem because those names will likely never change or change very, very rarely. 图像文件必须与thing.orgName的值相同的名称,但是对于我的用例来说,这不是问题,因为这些名称可能永远不会改变或非常非常少地改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM