在django中从单独的python文件返回函数
我有一个项目,只是创建了一个情节。 生成此绘图的代码存储在名为在django中从单独的python文件返回函数,python,django,Python,Django,我有一个项目,只是创建了一个情节。 生成此绘图的代码存储在名为theplotfunction的函数的theplot.py中 我试图做的是通过按下我的index.HTML中的HTML按钮向用户显示此绘图,但它没有加载。 我使用theplot.py创建绘图: THEPLOT.PY: def theplotfunction(request): import matplotlib.pyplot as plt import numpy as np import io fro
theplotfunction
的函数的theplot.py
中
我试图做的是通过按下我的index.HTML
中的HTML
按钮向用户显示此绘图,但它没有加载。
我使用theplot.py
创建绘图:
THEPLOT.PY:
def theplotfunction(request):
import matplotlib.pyplot as plt
import numpy as np
import io
from django.shortcuts import render
plt.plot(range(10))
fig = plt.gcf()
buf = io.BytesIO()
fig.savefig(buf,format='png')
buf.seek(0)
string = base64.b64encode(buf.read())
uri = urllib.parse.quote(string)
return render(request, 'plot.html',{'data':uri})
然后我将我的url链接到views.py
中的函数
URL.PY:
path('plot',views.plot,name='plot'),
然后views.py
plot函数将转到theplot.py
运行代码以生成绘图
VIEWS.PY:
`from .theplot import theplotfunction`
def plot(request):
return (theplotfunction)
Myindex.html
显示要运行的视图连接功能
INDEX.HTML:
<input type="button" value="plot" onclick="window.open('plot')">
我的问题是我点击按钮生成绘图,我得到一个
function'对象没有属性'get'
错误。当我在views.py
中生成图形时,代码工作正常,因此我知道图形代码很好。但是,当我更改它,使绘图函数由Plot.py生成时,它似乎不起作用。我觉得问题在于在views.py
中返回绘图函数。我已经玩过了,但似乎无法让它工作。我想你的意思是返回lotfunction(请求)
?谢谢你修复了它我想你的意思是返回lotfunction(请求)
?谢谢你修复了它
<img src="data:image/png;base64,{{ data }}" alt="" height="250" ,width="250">