从视图调用初始化数据库函数

2024-10-02 04:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的web应用程序有一个允许用户上传文件的模板。文件的内容存储在变量中

我希望将文件传递给视图(views.py),视图反过来调用initializedb.py脚本中的函数。我只想执行这个函数,而不是执行整个initializedb.py脚本(它涉及许多插入)。我的initializedb函数接收一个.csv文件作为输入,并将其内容插入数据库

我的问题是如何通过视图调用initializedb.py中的函数?

在我的template:- 你知道吗

$.post(
    "{{request.route_url('passer_view')}}", // view that should invoke the insert function
    {'data': filecontents} //file contents will be my global variable that holds the contents of the file
);  

我的观点

@view_config(route_name='passer_view' renderer="mytem.pt")
def insert(request):
    data = request.POST["data"]
    csvify_data(data) #this function makes renames the data into a csv file
    ###i should invoke the initializedb.py function here
    return HTTPFound(location=...)

我的初始化b.py:-

def main(argv=sys.argv):
    #a whole load of engine creation and stuffs
    with transaction.manager:
        def insertfunction(csvfile):
            DBSession.add(...)

这怎么办?有没有其他方法来做我正在做的事?任何想法都很感激。 提前谢谢


Tags: 文件csvthe函数py脚本view视图
1条回答
网友
1楼 · 发布于 2024-10-02 04:29:57

听起来你需要重新构造初始化的b.py。在另一个函数中定义的函数不能从外部访问。最好在模块级定义它们,并在主函数中调用它们:然后您还可以导入它们并从其他任何地方调用它们

相关问题 更多 >

    热门问题