【问题标题】:Django how to import value in request function to a function?Django如何将请求函数中的值导入函数?
【发布时间】:2020-06-17 13:45:10
【问题描述】:

我的项目结构如下:

_stato_patrimoniale
___views.py

_iva
___utility.py

在 stato patrimonale 我有以下代码:

from iva.utility import my_func 

def stato_patrimoniale(request):
    now=datetime.datetime.now()
    last_account_year=float(now.year)-1 #definizione dell'ultimo anno contabile
    now = now.year
    if request.method == 'POST':
        year = request.POST['year']
        if year != '':
            now = year
            last_account_year = float(now) - 1

    data=my_func()
    iva_a_credito=data['iva_a_credito']

现在在我的iva.utility 我有以下代码:

def my_func():
  pass

现在我想从stato_patrimoniale 导入my_func()last_account_year 的值。怎么可能?

【问题讨论】:

    标签: python python-3.x django django-models django-forms


    【解决方案1】:

    将值作为参数传递

    views.py:

    from iva.utility import my_func
    
    def stato_patrimoniale(request):
        now=datetime.datetime.now()
        last_account_year=float(now.year)-1 #definizione dell'ultimo anno contabile
        now = now.year
        if request.method == 'POST':
            year = request.POST['year']
            if year != '':
                now = year
                last_account_year = float(now) - 1
    
        data=my_func(last_account_year)
        iva_a_credito=data['iva_a_credito']
    

    其他文件:

    def my_func(last_account_year)
        // do something with last_account_year
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2012-08-17
      • 2020-08-18
      • 2019-10-31
      相关资源
      最近更新 更多