我相信我对这个问题有一些贡献。
对我来说奇怪的是,我的代码在我使用它的方式上是有意义的,但它不起作用。
如果在我的网址中,我尝试了以下操作。 helloworld 是我的 django 应用程序名称。
import helloworld
...
url(r'^test', helloworld.views.home1() , name='home'),
它会产生错误。即使从技术上讲,每件事都是正确的。我已经导入了我的应用程序,这是一个由 django manage startapp 自动创建的 python 模块
module' object has no attribute 'views'
我在 github 上找到了 django project site 的源代码,并查看了他们如何在应用程序的 urls 部分进行导入。去看看这个项目在 github 上。是大型项目实施的绝佳参考。那里有很多东西要学。
https://github.com/django/djangoproject.com.
这就是他们进行导入和 url 配置的方式。
from accounts import views as account_views
...
url(r'^~(?P<username>[\w-]+)/$', account_views.user_profile, name='user_profile'),
所以我将我的代码修改为类似的东西
import helloworld.views as helloView
...
url(r'^test', helloView.home1 , name='home'),
这很可能与 app/project/python 命名空间有关。我不完全确定。但是我的代码按预期工作,我仍然可以在各自的命名空间中拥有不同的应用程序。我只需要确保 import app.view as somename 在 app / project / python 命名空间方案中是唯一的。