gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:31:17 25 4
gpt4 key购买 nike

我正在处理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'

最佳答案

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

from contacts import forms

或者你可以尝试

import .forms

关于python - Django 自定义表单 ImportError 即使文件位于同一目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20029305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com