所以我的视图文件变得很大,我决定把它分成更小的部分 遵循此answer
我创建了一个views文件夹,在这个文件夹下创建了几个view ... py文件
现有的东西在拆分后仍然有效,但现在当我尝试创建新的视图文件时,我在url文件中遇到问题
这是我的_init_.py
文件,位于我的视图文件夹
from views import *
from viewscategory import *
from viewssubcategory import *
from viewsitemgroup import *
from viewsmaterial import *
from viewsbomversion import *
from viewsbom import *
from viewsapprovedmanufacture import *
这是我的网址文件相关部分
from django.conf.urls import url, include,patterns
import item.views
from views import *
urlpatterns = patterns('item.views',
url(r'^approvedmanufacture/new/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_new, name="approvedmanufacture_new"),
url(r'^approvedmanufacture/edit/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_edit, name="approvedmanufacture_edit"),
url(r'^approvedmanufacture/delete/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_delete, name="approvedmanufacture_delete"),
url(r'^approvedmanufacture/approvedmanufacture_details/(?P<pk>\d+)$', item.views.approvedmanufacture_details, name="approvedmanufacture_details"),
)
这是终端的错误
File "C:\Users\I812624\dev\mrp\src\item\urls.py", line 58, in <module>
url(r'^approvedmanufacture/new/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.appro
vedmanufacture_new, name="approvedmanufacture_new"),
AttributeError: 'module' object has no attribute 'approvedmanufacture_new'
[03/May/2016 17:51:10]"GET /item/material/material_bomversion_details/3 HTTP/1.1
" 500 59
即使我在ulr.py中修改我的行以包含完整路径,我仍然会得到相同的错误
url(r'^approvedmanufacture/new/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.viewsapprovedmanufacture.approvedmanufacture_new, name="approvedmanufacture_new"),
或
url(r'^approvedmanufacture/new/(?P<pk>\d+)/(?P<uri>\S+)/$', item.viewsapprovedmanufacture.approvedmanufacture_new, name="approvedmanufacture_new"),
同一个文件中有趣的东西我有2个观点:
-def material_bomversion_details 在我分裂之前,我有这个。
和新的 -def material_am_details
所以当我在shell中执行时 来自item.views导入material_bomversion_details它运行正常, 但是当我从item.views执行import material_am_details。
我收到ImportError:无法导入名称material_am_details
这超出了我的理解范围 看起来Django框架中存在某种缓存,可以保存分割前的所有内容,并且不会添加任何新内容。将所有内容移回一个视图文件后,一切正常。
答案 0 :(得分:1)
在views/__init__.py
中,您可以通过相对导入视图来解决此问题:
from .viewscategory import *
from .viewssubcategory import *
from .viewsitemgroup import *
from .viewsmaterial import *
from .viewsbomversion import *
from .viewsbom import *
from .viewsapprovedmanufacture import *
然后在你的网址中:
from django.conf.urls import url
from item import views
urlpatterns = [
url(r'^approvedmanufacture/new/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_new, name="approvedmanufacture_new"),
url(r'^approvedmanufacture/edit/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_edit, name="approvedmanufacture_edit"),
url(r'^approvedmanufacture/delete/(?P<pk>\d+)/(?P<uri>\S+)/$', item.views.approvedmanufacture_delete, name="approvedmanufacture_delete"),
url(r'^approvedmanufacture/approvedmanufacture_details/(?P<pk>\d+)$', item.views.approvedmanufacture_details, name="approvedmanufacture_details"),
]