我正在构建一个Django项目。在django项目中我有两个应用程序。一个是用户,一个是帐户。我想从用户应用程序中名为user_synapse的acounts app调用一个视图。我已导入帐户视图python文件以导入所有方法。我正在调用该方法,但我收到一个错误,指出我试图调用的视图方法未定义。它工作得比较早,但现在根本不起作用。我将django应用程序名称添加到设置文件和主URL文件中。这是我的代码:
用户app:
views.py文件:
#import all references from other apps
from accounts.views import *
from groups.models import *
from groups.views import *
...
# create synapse user
user_synapse(request)
帐户app:
views.py文件:
#import all references from other apps
from groups.models import *
from groups.views import *
from users.views import *
from users.models import *
# synapse import statements that are needed
from synapse_pay_rest import Client, Node, Transaction
from synapse_pay_rest import User as SynapseUser
from synapse_pay_rest.models.nodes import AchUsNode
...
# ensure someone is logged in
@login_required
# create a synapse user
def user_synapse(request):
# grab the logged in user
user = request.user
# grab users profile
profile = Profile.objects.get(user = user)
# set user items before creating items
legal_name = profile.first_name + " " + profile.last_name
note = legal_name + " has just created his synapse profile "
supp_id = generate_number()
cip_tag = user.id
# grab the account type
account = profile.business
if account == 'INDIVIDUAL':
account = False
if account == 'BUSINESS':
account = True
# the following is all of the information that is required in order to make a
# new user within the Synapse application
args = {
'email':str(user.email),
'phone_number':str(profile.phone),
'legal_name':str(legal_name),
'note': str(note),
'supp_id':str(supp_id),
'is_business':account,
'cip_tag':cip_tag,
}
# the following is the request to the synapse api as well as the returned
# json that contains information that needs to ba saved in local database
create_user = SynapseUser.create(client, **args)
response = create_user.json
# the following updates the current profile to add the users synapse id within
# the local database.
if response:
synapse_id = response['_id']
updateProfile = profile
updateProfile.synapse_id = synapse_id
updateProfile.save()
视图确实存在。世界上为什么说这个方法没有定义:
NameError at /personal/
name 'user_synapse' is not defined
settings.py文件:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'general',
'users',
'localflavor',
'groups',
'accounts',
]
它工作得比较早,但现在它不起作用,我不明白为什么。
答案 0 :(得分:1)
问题是你有一个循环导入:在users.views.py
from accounts.views import *
在accounts.views.py
中from users.views import *
Python中不允许这样做。有关详细信息,请参阅Python中的import dependency。