无法从其他应用导入视图

时间:2017-12-13 20:29:05

标签: django

我尝试将视图从一个应用导入到另一个应用中,但它给了我这个错误:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/trie/Desktop/django/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/trie/Desktop/django/myproject/myproject/urls.py", line 20, in <module>
    path('home/',include('wapperapp.urls')),
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/home/trie/Desktop/django/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/trie/Desktop/django/myproject/wapperapp/urls.py", line 3, in <module>
    from . import views
  File "/home/trie/Desktop/django/myproject/wapperapp/views.py", line 2, in <module>
    from myproject.vidbrowser.views import index
ImportError: No module named 'myproject.vidbrowser'

我在项目的其他部分进行了这样的导入,但是没有给出任何错误,但这个确实...

我确信我尝试导入的内容存在,但我似乎无法找出它无法正常工作的原因。

有谁知道这里的问题是什么?

2 个答案:

答案 0 :(得分:2)

外部myproject目录(包含manage.py)位于Python路径上。因此导入应该是:

from vidbrowser.views import index

答案 1 :(得分:0)

试试这个

from appname import views

然后

views.index

from appname.views import index