使用Django if语句在网页上运行JavaScript
我只想在数据输出出来时显示图像。因此,只有当使用Django if语句在网页上运行JavaScript,javascript,python,html,django,Javascript,Python,Html,Django,我只想在数据输出出来时显示图像。因此,只有当数据存在时,才应执行此JavaScript函数。代码如下: {% load static %} <html> <head> <title> Python button script </title> </head> <body> {% if data %} {{data | safe}} {% endif %} <form action="/external/&q
数据
存在时,才应执行此JavaScript函数。代码如下:
{% load static %}
<html>
<head>
<title>
Python button script
</title>
</head>
<body>
{% if data %}
{{data | safe}}
{% endif %}
<form action="/external/" method="post">
{% csrf_token %}
Input Text:
<input type="text" name="param" required><br><br>
<p id="demo" style="color:red">{{data_external}} {{data1}}
</p>
<img id="pic" src="{% static 'images/graph.png' %}" style="display:none;" />
<input type="submit" onclick="reloadimg();" value="Execute External Python Script">
</form>
</body>
</html>
<script>
function reloadimg(){
var src = "{% static 'images/graph.png' %}";
imageObject = document.getElementById("pic");
imageObject.style.display = 'inline-block';
imageObject.src = src;
}
</script>
{%load static%}
Python按钮脚本
{%if数据%}
{{数据|安全}
{%endif%}
{%csrf_令牌%}
输入文本:
{{data\u external}{{data1}}
函数重载img(){
var src=“{%static”images/graph.png%}”;
imageObject=document.getElementById(“pic”);
imageObject.style.display='inline block';
imageObject.src=src;
}
我怎样才能达到这样的结果呢?如果这个问题有什么需要补充的,请告诉我。将您的
img
标记包装在{%If data%}
和{%endif%}
中。这不是所需的行为吗?如果没有数据,就没有图像。或者你的问题是为什么数据是空的/不存在?将你的img
标记包装在{%if data%}
和{%endif%}
中?这不是你想要的行为吗?如果没有数据,就没有图像。或者你的问题是为什么数据是空的/不存在?