在我的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__'
THX
答案 0 :(得分:0)
我认为如果你发布你的视图以及你在html中添加的表单的来源会有所帮助。一种可能性是您在视图中切换了request.POST信息,但需要查看。
小细节但是form method = post应该是form method =“post”到处都正确吗?
答案 1 :(得分:0)
我通过在静态文件夹中创建两个子目录并更改在计算文件中保存文件的路径(上面未显示)来解决了这个问题。因此,两种功能的图像输出不会相互干扰。