我想使用plotly.js 库绘制图表,并且图表的输入是通过html 表单从用户处获取的。
我创建了一个 html 表单来读取绘图的 X 和 Y 值。在views.py中,我使用request.POST.get访问表单数据,然后将其添加到列表x[],y[]中。
from django.shortcuts import render
x=[]
y=[]
def index(request):
return render(request,'home.html')
def inputplot(request):
if(request.method=="POST"):
xvalue=request.POST.get('xvalue')
yvalue=request.POST.get('yvalue')
x.append(xvalue)
y.append(yvalue)
print x
print y
return render(request,'inputplot.html')
del x[:]
del y[:]
return render(request,'inputplot.html')
def plot(request):
`return render(request,'plot.html',{'x':x,'y':y})
我想要plot.html 中的列表,但是当我将数据附加到列表时,它会自动添加一些字符。例如,我为 x 值输入数字 12,但该数字在列表中显示为 [u'12']。
为什么会发生这种情况?
请给我一个解决方案或向我展示另一种读取用户数据并绘制值的方法。???帮帮我
最佳答案
来自 request.POST
的数据在这里是一个字符串。 [u'12']
告诉您 it's a unicode string. ,这就是它无法正确绘制的原因。
假设它们都是整数,您需要以下内容:
xvalue=int(request.POST.get('xvalue'))
如果合适的话,用十进制替换。
关于javascript - Django 使用plotly 和表单中的数据绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42732641/