django - 在许多视图中重用功能 [英] django - reusing functions in many views

查看:110
本文介绍了django - 在许多视图中重用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我在一些视图中创建的功能,必须在许多其他视图中重用。我需要创建一个类并将这些函数放在类中吗?如果是,在Django中究竟需要做些什么,然后在新视图中如何调用和启动它们?

I have a bunch of functions that I created in some views that must be reused in many other views. Do I need to create a class and put those functions in a class? If yes how exactly has to be done in Django and then how do I call and initiate them in the new views?

推荐答案

Django视图只是Python函数。您可以从其他Python代码调用其他Python函数。将你的函数放入一个.py文件中,导入它并调用这些函数。

Django views are just Python functions. You can call other Python functions from them just as you can from any other Python code. Put your functions into a .py file, import it, and invoke the functions.

当然,创建一个类来保存函数的其他原因可能是有道理的,但是您一定不需要为了从视图中调用它们。

Of course, it may make sense for other reasons to create a class to hold the functions, but you certainly don't need to in order to call them from views.

这篇关于django - 在许多视图中重用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆