【问题标题】:view in one app is not recognized by different view - Django不同视图无法识别一个应用程序中的视图 - Django
【发布时间】:2017-11-23 12:12:23
【问题描述】:

我正在构建一个 Django 项目。在 django 项目中,我有两个应用程序。一个是用户,一个是帐户。我想在用户应用程序中调用名为 user_synapse 的帐户应用程序的视图。我已导入帐户视图 python 文件以导入所有方法。然后我正在调用该方法,但我收到一个错误,指出我试图调用的视图方法未定义。它以前可以工作,但现在根本不工作。我将 django 应用程序名称添加到设置文件和主 urls 文件中。这是我的代码:

用户应用:

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)

帐户应用程序:

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',
]

它之前工作过,但现在不工作了,我不知道为什么。

【问题讨论】:

  • 您是否在accounts urls.py 文件中添加了视图?我认为您需要检查您的应用的正确网址
  • 不是真正解决您的问题,但似乎您在某个地方遇到了导入问题...避免import * 是避免此类问题的第一步。您可能想阅读:stackoverflow.com/questions/2386714/why-is-import-bad
  • 是的,这就是它的样子url(r'^admin/', admin.site.urls), url(r'^accounts/', include('accounts.urls')), url(r'^groups/', include('groups.urls')), url(r'^', include('users.urls')),
  • 我尝试在端口声明中输入它,它说找不到...
  • 错误提示“/personal/”,但我没有看到任何个人网址设置

标签: django django-views


【解决方案1】:

问题是你有一个循环导入:在 users.views.py 中

from accounts.views import *

在accounts.views.py中

from users.views import *

这在 Python 中是不允许的。有关更多信息,请参阅 Python 中的 import dependency

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2017-10-23
    • 2022-01-22
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多