Django:如何导入函数

时间:2019-01-08 15:43:08

标签: django

Python 3.6.7

Django 2.1.5

在PyCharm社区版中,我创建了一个名为simple_pr的新项目。

然后我组织了一个Django项目:

django-admin startproject simple_project

然后申请:

python manage.py startapp home

我的Python路径:

/home/michael/PycharmProjects/simple_pr/simple_project
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/PycharmProjects/simple_pr
/opt/pycharm-community-2018.3.2/helpers/third_party/thriftpy
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/.PyCharmCE2018.3/system/cythonExtensions
/home/michael/PycharmProjects/simple_pr/simple_project
/usr/lib/python36.zip
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg

项目结构:

(venv)michael @ michael:〜/ PycharmProjects / simple_pr $树

.
├── simple_project
│   ├── home
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── manage.py
│   └── simple_project
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-36.pyc
│       │   └── settings.cpython-36.pyc
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── venv

urls.py

from django.contrib import admin
from django.urls import path
from simple_project.home.views import current_datetime

urlpatterns = [
    path('admin/', admin.site.urls),
]

home.views.py

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
]

跟踪:

/home/michael/PycharmProjects/simple_pr/venv/bin/python /opt/pycharm-community-2018.3.2/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 37559 --file /home/michael/PycharmProjects/simple_pr/simple_project/manage.py runserver
pydev debugger: process 5982 is connecting

Connected to pydev debugger (build 183.4886.43)
pydev debugger: process 5988 is connecting

Performing system checks...

Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x7f67b10a5940>
Traceback (most recent call last):
  File "/opt/pycharm-community-2018.3.2/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 632, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/michael/PycharmProjects/simple_pr/simple_project/simple_project/urls.py", line 18, in <module>
    from simple_project.home.views import current_datetime
ModuleNotFoundError: No module named 'simple_project.home'
/home/michael/PycharmProjects/simple_pr/simple_project
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/PycharmProjects/simple_pr
/opt/pycharm-community-2018.3.2/helpers/third_party/thriftpy
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/.PyCharmCE2018.3/system/cythonExtensions
/home/michael/PycharmProjects/simple_pr/simple_project
/usr/lib/python36.zip
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg

好吧,换句话说,问题出在这里:

from simple_project.home.views import current_datetime
    ModuleNotFoundError: No module named 'simple_project.home'

顺便说一句,PyCharm本身向我建议了如何使用其代码完成功能将其导入。

您能帮助我了解我的进口商品怎么了吗?

1 个答案:

答案 0 :(得分:1)

您不应在模块路径中包含项目名称。

from home.views import current_datetime