我对这里提出的问题有同样的问题: Django custom form ImportError even though file is in the same directory
这是来自django应用程序中的urls.py:
import bulkEdit
...
...
urlpatters = patterns('',
url(r'^engine/$', component_views.engine, name='engine'),
...
url(r'^admin/', include(bulkEdit.urls)),
我的bulkEdit.py文件与urls.py在同一目录中。
我得到的错误是
File "/home/context/work/riot/src/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named bulkEdit
如果我尝试
,我会得到同样的错误from bulkEdit import urls as bulkEditUrls
...
...
urlpatters = patterns('',
url(r'^engine/$', component_views.engine, name='engine'),
...
url(r'^admin/', include(bulkEditUrls)),
bulkEdit是与urls.py文件位于同一目录中的文件;文件结构是
Rapier
|-component
| |-__init__.py
| |-admin.py
| |-forms.py
| |-models.py
| |-views.py
|
|-Chassis
| |-__init__.py
| |-urls.py
| |-bulkEdit.py
| |-settings.py
| |-views.py
|
|-manage.py
这是我到目前为止所尝试的内容(在所有这些情况下,'Chassis'
都在INSTALLED_APPS
):
使用Python 2.7,因此我的
语法错误import .bulkEdit
我也试过了:
url(r'^admin/', include(Chassis.bulkEdit.urls)),
给了我NameError: name 'Chassis' is not defined
url(r'^admin/', include("Chassis.bulkEdit.urls")),
给了我ImportError: No module named urls
url(r'^admin/', include("Chassis.bulkEdit")),
给了我ImproperlyConfigured: The included urlconf <module 'Chassis.bulkEdit' from '/home/userag/work/project/src/project/Chassis/bulkEdit.pyc'> doesn't have any patterns in it
url(r'^admin/', include(Chassis.bulkEdit)),
给了我NameError: name 'Chassis' is not defined
当我有
时import bulkEdit
...
test = url(r'^admin/', include(bulkEdit.urls))
只要它不在urlpatterns中,我就不会收到任何错误。当我将test
添加到urlpatterns
urlpatterns = patterns('',
url(r'^engine/$', component_views.engine, name='engine'),
...
test
我收到错误。由于我使用管理表格做事,我还需要导入bulkEdit吗?
答案 0 :(得分:2)
尝试相对导入:
from .bulkEdit import urls as bulkEditUrls
答案 1 :(得分:1)
更新:评论后
尝试: url(r'^ admin /',include(“Chassis.bulkEdit.urls”))
但首先创建bulkEdit包并在其中放入urls.py文件。然后将bulkEdit.py中的代码复制到
__init__.py
包的文件,以便与代码的其他部分向后兼容。
问题可能与bulkEdit.urls不是python模块这一事实有关。
更改后的目录结构:
Rapier
|-component
| |-__init__.py
| |-admin.py
| |-forms.py
| |-models.py
| |-views.py
|
|-Chassis
| |-__init__.py
| |-urls.py
| |-bulkEdit
| |-__init__.py
| |-urls.py
| |-settings.py
| |-views.py
|
|-manage.py
答案 2 :(得分:1)
错误是我在bulkEdit / urls.py中的实际网址格式
urlpatterns = patterns('',
(r'(?P<app_name>[^/])/(?P<model_name>[^/]+)-masschange/(?P<object_ids>[0-9,]+)/$', 'bulkEdit.mass_change_view'),)
应该是
urlpatterns = patterns('',
(r'(?P<app_name>[^/])/(?P<model_name>[^/]+)-masschange/(?P<object_ids>[0-9,]+)/$', 'Chassis.bulkEdit.bulkEdit.mass_change_view'),)
No module named bulkEdit
的错误源自此处。
答案 3 :(得分:0)
但是django实际上是从其他目录中导入你的bulkEdit
,我认为包含文件夹的bulkEdit
不在sys.path
中(其他选项是__init__.py
缺席),因此错误。由于您的申请通常位于sys.path
,您可以尝试指定
url(r'^admin/', include('Chassis.bulkEdit.urls'))