Django自定义表单ImportError,即使文件位于同一目录中

时间:2013-11-17 09:48:17

标签: python django python-3.x django-forms django-1.6

我正在完成Effective Django's教程系列。我目前在尝试创建要在应用中使用的自定义表单时遇到问题。我按照this part of the tutorial中的说明创建了forms.py文件,并对views.py文件进行了更改。我的目录结构如下所示:

(project root)
|
├── address.db
├── addressbook
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── urls.py
│   └── wsgi.py
├── contacts
│   ├── __init__.py
│   ├── admin.py
│   ├── forms.py
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   └── views.py
├── manage.py
└── requirements.txt

问题是,当我尝试加载网站时,我收到以下错误:

Traceback (most recent call last):
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/handlers/base.py", line 101, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 318, in resolve
    for pattern in self.url_patterns:
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 346, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 341, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1534, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1024, in load_module
  File "<frozen importlib._bootstrap>", line 1005, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 870, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/Users/wtodom/.virtualenvs/tutorial/addressbook/urls.py", line 4, in <module>
    import contacts.views
  File "/Users/wtodom/.virtualenvs/tutorial/contacts/views.py", line 5, in <module>
    import forms
ImportError: No module named 'forms'

第5行是views.py中的一行,我在其中导入forms.py文件。代码片段(带有几行缓冲区)是:

from django.shortcuts import render
from django.views.generic import ListView, CreateView, UpdateView, DeleteView, DetailView
from django.core.urlresolvers import reverse
from contacts.models import Contact
import forms


class ListContactView(ListView):

    model = Contact
    template_name = 'contact_list.html'

正如您在目录树中看到的那样,应该可以访问forms.py文件。我不知道为什么我会收到错误。有什么想法吗?

编辑1

我尝试在最初发布之前将导入更改为from forms import ContactForm。我仍然有同样的错误:

  File "/Users/wtodom/.virtualenvs/tutorial/contacts/views.py", line 5, in <module>
    from forms import ContactForm
ImportError: No module named 'forms'

3 个答案:

答案 0 :(得分:3)

据我所知,Python 3中的导入系统发生了变化。只需要更具体地说明要导入的内容。我假设您要从forms.py

导入contacts
from contacts import forms

或者你可以尝试

import .forms

答案 1 :(得分:0)

这样做是正确的:

from forms import FormClassName

或者你可以

from forms import *

但没有必要。 从其他应用程序,您必须

from contacts.forms import FormClassName # or wildcard '*'

答案 2 :(得分:0)

导入python搜索导入的目录是项目根目录时,没有名为form的模块。

因此,如果您想测试在项目根目录中创建一个名为forms的文件,它将导入它。

以下方式肯定会有效

from contacts.forms import FormClassName # or wildcard '*'