Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Flask将值传递给函数_Python_Flask - Fatal编程技术网

Python 使用Flask将值传递给函数

Python 使用Flask将值传递给函数,python,flask,Python,Flask,我有一个base.py文件,其中有很多列表(超过50个) 另外,另一个func.py文件包含用于处理base.py文件中的url的函数 我需要从base.py文件派生链接,并从HTML模板中的func.py文件派生函数结果 <tr> <td>Model</td> <td><a href="{{ url.model1[0] }}" target="_blank">{{ price0 of func.py }}</a></

我有一个base.py文件,其中有很多列表(超过50个)

另外,另一个func.py文件包含用于处理base.py文件中的url的函数

我需要从base.py文件派生链接,并从HTML模板中的func.py文件派生函数结果

<tr>
<td>Model</td>
<td><a href="{{ url.model1[0] }}" target="_blank">{{ price0 of func.py }}</a></td>
<td><a href="{{ url.model1[1] }}" target="_blank">{{ price1 of func.py }}</a></td>
<td><a href="{{ url.model1[2] }}" target="_blank">{{ price2 of func.py }}</a></td>
<td><a href="{{ url.model1[3] }}" target="_blank">{{ price3 of func.py }}</a></td>
<td><a href="{{ url.model1[4] }}" target="_blank">{{ price4 of func.py }}</a></td>
<td><a href="{{ url.model1[5] }}" target="_blank">{{ price5 of func.py }}</a></td>
<td><a href="{{ url.model1[6] }}" target="_blank">{{ price6 of func.py }}</a></td>
</tr>
使用Flask,我输出链接如下

import base

def index():
    return render_template("index.html",
        url = base)
问题:如何将特定模型的所需url从base.py文件的特定列表传输到func.py文件函数,并使用Flask在HTML模板中显示此函数的结果

HTML模板

<tr>
<td>Model</td>
<td><a href="{{ url.model1[0] }}" target="_blank">{{ price0 of func.py }}</a></td>
<td><a href="{{ url.model1[1] }}" target="_blank">{{ price1 of func.py }}</a></td>
<td><a href="{{ url.model1[2] }}" target="_blank">{{ price2 of func.py }}</a></td>
<td><a href="{{ url.model1[3] }}" target="_blank">{{ price3 of func.py }}</a></td>
<td><a href="{{ url.model1[4] }}" target="_blank">{{ price4 of func.py }}</a></td>
<td><a href="{{ url.model1[5] }}" target="_blank">{{ price5 of func.py }}</a></td>
<td><a href="{{ url.model1[6] }}" target="_blank">{{ price6 of func.py }}</a></td>
</tr>

因此,您需要在视图函数中执行逻辑操作。
创建一个新的模型列表,每个模型的url和价格都关联在一起-在字典中
然后,创建新数据后,将其传递到模板,在模板中可以循环访问数据并访问值

如果每个url都有自己的功能,您可以将您的功能存储在列表中,并通过您循环使用的url索引访问相关功能
见此问题—

func.py

def url1 (murl):
#####################
    print(price)

def url2 (murl):
#####################
    print(price)
my_fn = [func1, func2, func3, etc]  
导入库
导入函数
def index():
带有链接的模型
对于基础中的模型:
#使用关联链接和价目表理解创建新的dict
new_model=[{“url”:link,“price”:my_fn[idx](link)}对于idx,枚举中的链接(model)]
带有链接的模型。附加(新模型)
返回render_模板(“index.html”,models=models_和链接)
然后可以在Jinja中使用循环 因此,您可以循环浏览包含多个模型的URL,然后循环浏览这些模型的URL:

<tr>
  <td>Model</td>
  {% for model_list in models %}
    {% for model in model_list %}
      <td>
        <a href="{{ model.url }}" target="_blank">
          {{ model.price }}
        </a>
      </td>
    {% endfor %}
  {% endfor %}
</tr>

模型
{%用于模型%}中的模型_列表
{模型列表%中的模型为%0}
{%endfor%}
{%endfor%}

您应该在
index
函数中执行大部分逻辑,然后返回一个字典或一个包含所需内容的字典列表,格式为
data={'url':url.model[0],'result':result\u of_func.py}
然后您可以在模板中访问这些内容。然后按原样传递它
return render\u template(“index.html”,data=data)
了解
func.py
的外观和功能会很有用。它是一个具有单一功能的模块吗?是否有各种各样的功能,并且这些模型中的每一个都需要用不同的模型进行分析?需要对您的问题有一个更好的定义,因为它并不真正清楚您需要解决问题的哪一部分。您可以在Jinja中创建自定义过滤器,将模板中的函数应用于您的值-@DavidGildour是否在
func.py
中执行函数只返回价格?它们之间有区别吗?
base.py
中的模型只是一个链接列表吗?如何从链接中获取价格?@Craicerjack 1模型-6个链接-6个不同的站点模型超过50个每个链接都有自己的功能,使用BeautifulSoup解析站点的价格。base是一个模块,因此pylint写入“对象不可重复”
dir(base)
返回无序列表,并添加函数
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu