简体   繁体   中英

Import a django app view in another app view

I have two django apps and I've called a view of app1 in app2, like this:

#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)

def view2(request):
  #some code

It works fine. My problem is that now I want to call a view from app2 in app1. So I add the import statement in someview.py like this:

#app: app1
    #someview.py
    from app2.otherview import view2

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

The result is an ImportError "cannot import name view2". Can anyone tell me why this happen?

The second import shadowing the first one... Try like

import app2.otherview 

or

from app2.views as app2_views 

You can't do this because makes a loop. You must add functions in another file and import in app1 and app2 separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM