在我的django框架中,我从表单中收集用户输入并创建一个matplotlib图像。在 html 文件中的标记字段中按下按钮后,将显示此图像。
每次按下按钮后,此图像都会更新、更改并新显示。这很好用。现在我打算在第二个标签上引入第二个图像,该图像在按下不同的按钮后显示和更改。
我遇到的问题是,按下第二个按钮后,第一个图像消失,按下第一个按钮后,第二个图像消失。这是 html 代码的一部分:
<form method=post action="">
{% csrf_token %}
<table>
{% for field in form %}
<tr>
<td>{{ field.label }}</td>
<td>{{ field }}</td>
</tr>
{% endfor %}
</table>
<br>
<p><input type=submit name='button1' value='Create' class="btn btn-default"></form></p>
</form>
<p>
{% if result != None %}
{% load static %}
<img src="{% get_static_prefix %}{{ result }}" width=1000>
{% endif %}
</p>
<form method="post" class="topright">
{{form3}}
{% csrf_token %}
<p><input type=submit name='button2' value='Update' class="btn btn-default" default=1></p>
</form>
<form method=post action="">
{% csrf_token %}
<table>
{% for field in form4 %}
<tr>
<td>{{ field.label }}</td>
<td>{{ field }}</td>
</tr>
{% endfor %}
</table>
<p><input type=submit name='button3' value='Predict' class="btn btn-default" default=1></p>
</form>
<p>
{% if predictresult != None %}
{% load static %}
<img src="{% get_static_prefix %}{{ predictresult }}" width=1000>
{% endif %}
</p>
图像文件通过以下方式保存到静态文件夹中: plt.savefig(plotfile( 中。大多数视图代码如下:
def bayes_create_and_update(request):
#os.chdir(os.path.dirname(__file__))
global bandit
global invgammaresult
global predictresult
invgammaresult = None
predictresult = None
if request.method == 'POST' and 'button1' in request.POST:
form = InputForm(request.POST)
if form.is_valid():
form2 = form.save(commit=False)
bandit = gauss_bandit(form2.m, form2.s)
invgammaresult=pull_and_update(bandit,1)
invgammaresult=invgammaresult[7:]
form3= Sample_InputForm()
form4=Predict_InputForm()
elif request.method == 'POST' and 'button2' in request.POST:
form3 = Sample_InputForm(request.POST)
if form3.is_valid():
form2 = form3.save(commit=False)
invgammaresult=pull_and_update(bandit,form2.n)
invgammaresult=invgammaresult[7:]
form = InputForm(request.POST)
form4=Predict_InputForm()
elif request.method == 'POST' and 'button3' in request.POST:
form4 = Predict_InputForm(request.POST)
if form4.is_valid():
form2 = form4.save(commit=False)
predictresult=bandit.predict(form2.p1,form2.p2)
predictresult=predictresult[7:]
form = InputForm(request.POST)
form3= Sample_InputForm()
else:
form = InputForm()
form3= Sample_InputForm()
form4=Predict_InputForm
return render ( request, 'C:/Users/Jan/PycharmProjects/Newversion/newattempt/online_comp/templates/bayes/bayes.html',
{'form': form,
'result': invgammaresult,
'form3':form3,
'form4':form4,
'predictresult':predictresult
})
产生表单的模型是以下代码:
from django.db import models
from django.forms import ModelForm
class Input(models.Model):
m = models.FloatField(
verbose_name=' Mean of the Normal Distribution:', default=0.0)
s = models.FloatField(
verbose_name=' Standard Deviation of the Normal Distribution:', default=1.0)
class InputForm(ModelForm):
class Meta:
model = Input
fields='__all__'
class Sample_Input(models.Model):
n = models.FloatField(
verbose_name=' Number of samples you wish to draw:', default=1)
class Sample_InputForm(ModelForm):
class Meta:
model=Sample_Input
fields='__all__'
class Predict_Input(models.Model):
p1 = models.FloatField(
verbose_name=' Lower bound', default=-1.0)
p2 = models.FloatField(
verbose_name=' Upper bound', default=1.0)
class Predict_InputForm(ModelForm):
class Meta:
model=Predict_Input
fields='__all__'
感谢
如果您发布您的视图以及您放入 html 中的表单的来源,这将很有帮助。一种可能性是你有请求。开机自检信息在视图中切换,但需要查看。
小细节,但表单方法=帖子应该是表单方法="帖子"到处都是对吗?
我已经通过在静态文件夹中创建两个子目录并更改在计算文件中保存文件的路径(上面未显示(来解决问题。因此,这两个函数的图像输出不会相互干扰。