Django:如何使用其他.py文件中定义的变量?

问题描述 投票:0回答:1

我想在其他文件plots.py中使用x以下作为函数get_price()中的变量。]​​>

views.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)

plot.py

def get_price():
    input_x = x + 1
    return input_x

但是它不起作用。我应如何为此目的描述功能?关键是,我稍后需要通过views.py将返回值用于模板。

我想在x下面使用其他文件plot.py中的get_price()函数中的变量。 views.py类ScatterView(TemplateView):def get(自己,请求,* args,** kwargs):context = ...

python django function view arguments
1个回答
0
投票

为什么不只是通过?将您的代码更改为如下所示:


-1
投票

一旦有了变量x。您需要在函数中传递它。

© www.soinside.com 2019 - 2024. All rights reserved.