开发者

django import a view function

开发者 https://www.devze.com 2023-01-21 19:58 出处:网络
I have a django application xxx which does a number of things. I also have a sepaerate application yyy. Which wants to call one of the functions of xxx.

I have a django application xxx which does a number of things.

I also have a sepaerate application yyy. Which wants to call one of the functions of xxx.

Is there a way for me to import the functions?

For开发者_如何学C example, in yyy can i say

from toplevel.xxx import doit

Or what is the best approach, I dont want to duplicate code.


Of course, you can fo it.

With a proper import and parameter, you can do it.

#app: app1
#someview.py

def a_view(request, someparam):
    #some code here


#app: app2
#otherview.py

from app1.someview import a_view

def another_view(request):
param = 1
a_view(request, param)

As for an example

UPDATE: Wish to mention that, your function a_view() do not have to get a parameter at all. So you can call functions with no paramaters. I just wish to mention that, if your function have paramaters, you have to pass them as if you do within an application.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号