Django使用来自表单的数据绘制图形



我想使用plotly.js库绘制agraph,并且图表的输入是通过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中的列表,但是当我将数据附加到列表中时,它会自动添加一些字符。例如,i输入x值的数字12,但是此数字显示为[u'12']。

为什么发生这种情况?

请给我一个解决方案或向我展示另一种读取用户数据并绘制值的方法。帮助我

request.POST的数据在这里是字符串。[u'12']告诉您这是一个Unicode字符串。这就是为什么它无法正确绘制的原因。

假设它们都是整数,您需要以下内容: xvalue=int(request.POST.get('xvalue'))

如果适当的话,用十进制代替。

最新更新