我想在另一个文件 plots.py 中的函数 get_price() 中使用下面的 x 作为变量。
视图.py
class ScatterView(TemplateView) :
def get(self, request, *args, **kwargs) :
context = super().get_context_data(**kwargs)
context['form'] = SampleForm()
x = request.GET.get('x')
context['calculated_x'] = plots.get_price()
return render(request, 'index.html', context)
绘图.py
def get_price():
input_x = x + 1
return input_x
但它不起作用。为此,我将如何描述该功能?关键是,我稍后需要通过views.py 使用模板的返回值。
为什么不直接通过?将您的代码更改为如下所示:
def get_price(x):
input_x = x + 1
return input_x
将其导入类,如下所示:
import plots
将其添加到您的代码中,如下所示:
class ScatterView(TemplateView) :
def get(self, request, *args, **kwargs) :
context = super().get_context_data(**kwargs)
context['form'] = SampleForm()
x = request.GET.get('x')
context['calculated_x'] = plots.get_price(x)
return render(request, 'index.html', context)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句