Javascript 如何根据Django中的表单输入向用户显示生成的图像?
我目前正在使用对象的属性通过matplotlib生成一个图像,并且能够创建一个视图,在HttpResponse中显示所述图像。我使用了以下代码段来执行此操作:。我还将URL配置为在导航到domain.com/objectID.png时成功生成和显示图像 现在我想创建一个表单,它的输入将用于从matplotlib生成图像。然后我想在表单的同一页上向用户显示生成的图像 如何将表单的输入反馈到matplotlib图像生成器,然后将结果图像显示回用户 非常感谢你的帮助 致意 编辑1: 我正在寻找解决方案,我相信我会在模板中显示如下图像。不确定如何输入变量“img”:Javascript 如何根据Django中的表单输入向用户显示生成的图像?,javascript,jquery,python,django,matplotlib,Javascript,Jquery,Python,Django,Matplotlib,我目前正在使用对象的属性通过matplotlib生成一个图像,并且能够创建一个视图,在HttpResponse中显示所述图像。我使用了以下代码段来执行此操作:。我还将URL配置为在导航到domain.com/objectID.png时成功生成和显示图像 现在我想创建一个表单,它的输入将用于从matplotlib生成图像。然后我想在表单的同一页上向用户显示生成的图像 如何将表单的输入反馈到matplotlib图像生成器,然后将结果图像显示回用户 非常感谢你的帮助 致意 编辑1: 我正在寻找解决方案
{% if img %} <img border="0" alt="My Generated Image" src="{{ img }}" /> {% endif %}
{%if img%}{%endif%}
您需要向服务器发出AJAX请求,通过javascript提供一些url(可能需要借助jQuery之类的库)。请求将传递表单中的数据并返回到相应图像的链接。假设您希望在用户输入x
和y
坐标后生成一个简单的图形/图像
要求:
<form method="POST" action="/generate-image/" id="GenImgForm">
<input type="text" name="x-coordinate" />
<input type="text" name="y-coordinate" />
</form>
<!-- Make an empty div where the returned image will be shown -->
<div id="ShowImage"></div>
<script type="text/javascript" src="/static/path/to/jquery.js"></script>
<script type="text/javascript" src="/static/path/to/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
target: '#ShowImage',
};
$("#GenImgForm").ajaxForm(options);
return false;
});
</script>
from matplotlib import pyplot as plt
import io
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
sio = io.BytesIO()
plt.savefig(sio, format="png")
encoded_img = base64.b64encode(sio.getvalue())
return HttpResponse(encoded_img)
<img id="plt" src=""></img>
$.ajax(
{
headers: {"X-CSRFToken":token},
type: "GET",
url: "ajax/show_hcPlot",
success: function(response){
$("#plt").attr('src',"data:image/png;base64, " + response);
}
}
)
HTML:
<form method="POST" action="/generate-image/" id="GenImgForm">
<input type="text" name="x-coordinate" />
<input type="text" name="y-coordinate" />
</form>
<!-- Make an empty div where the returned image will be shown -->
<div id="ShowImage"></div>
<script type="text/javascript" src="/static/path/to/jquery.js"></script>
<script type="text/javascript" src="/static/path/to/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
target: '#ShowImage',
};
$("#GenImgForm").ajaxForm(options);
return false;
});
</script>
from matplotlib import pyplot as plt
import io
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
sio = io.BytesIO()
plt.savefig(sio, format="png")
encoded_img = base64.b64encode(sio.getvalue())
return HttpResponse(encoded_img)
<img id="plt" src=""></img>
$.ajax(
{
headers: {"X-CSRFToken":token},
type: "GET",
url: "ajax/show_hcPlot",
success: function(response){
$("#plt").attr('src',"data:image/png;base64, " + response);
}
}
)
我的答案与公认的答案几乎相同,但工作上几乎没有变化 views.py:
<form method="POST" action="/generate-image/" id="GenImgForm">
<input type="text" name="x-coordinate" />
<input type="text" name="y-coordinate" />
</form>
<!-- Make an empty div where the returned image will be shown -->
<div id="ShowImage"></div>
<script type="text/javascript" src="/static/path/to/jquery.js"></script>
<script type="text/javascript" src="/static/path/to/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
target: '#ShowImage',
};
$("#GenImgForm").ajaxForm(options);
return false;
});
</script>
from matplotlib import pyplot as plt
import io
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
sio = io.BytesIO()
plt.savefig(sio, format="png")
encoded_img = base64.b64encode(sio.getvalue())
return HttpResponse(encoded_img)
<img id="plt" src=""></img>
$.ajax(
{
headers: {"X-CSRFToken":token},
type: "GET",
url: "ajax/show_hcPlot",
success: function(response){
$("#plt").attr('src',"data:image/png;base64, " + response);
}
}
)
html:
<form method="POST" action="/generate-image/" id="GenImgForm">
<input type="text" name="x-coordinate" />
<input type="text" name="y-coordinate" />
</form>
<!-- Make an empty div where the returned image will be shown -->
<div id="ShowImage"></div>
<script type="text/javascript" src="/static/path/to/jquery.js"></script>
<script type="text/javascript" src="/static/path/to/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
target: '#ShowImage',
};
$("#GenImgForm").ajaxForm(options);
return false;
});
</script>
from matplotlib import pyplot as plt
import io
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
sio = io.BytesIO()
plt.savefig(sio, format="png")
encoded_img = base64.b64encode(sio.getvalue())
return HttpResponse(encoded_img)
<img id="plt" src=""></img>
$.ajax(
{
headers: {"X-CSRFToken":token},
type: "GET",
url: "ajax/show_hcPlot",
success: function(response){
$("#plt").attr('src',"data:image/png;base64, " + response);
}
}
)
很好的回答,非常感谢。如果你能教我如何使用JavaScript函数,我会非常感激。实际上,所有的JS代码基本上都是为了访问jQuery的API和jQuery表单插件。因此,您不必真正担心它,因为处理表单的所有代码都已在该插件中编写。你只需要写几行代码来访问他们的API,我就是这么做的。阅读此文档:在开始工作后接受答案。非常感谢你的帮助!作为未来读者的补充,我认为表单ID应该从“#GenImgForm”改为“GenImgForm”。谢谢。我很高兴这有帮助。感谢您指出表单的
id
中的错误。我现在已经修好了。