django包含奇怪的行为

时间:2016-11-22 16:37:14

标签: django

我用这些命令创建了一个非常简单的django项目(python 2.7):

{
  "private": true,
  "scripts": {
    "prod": "gulp --production",
    "dev": "gulp watch"
  },
  "devDependencies": {
    "bootstrap-sass": "^3.3.7",
    "gulp": "^3.9.1",
    "jquery": "^3.1.0",
    "laravel-elixir": "^6.0.0-11",
    "laravel-elixir-vue-2": "^0.2.0",
    "laravel-elixir-webpack-official": "^1.0.2",
    "lodash": "^4.16.2",
    "vue": "^2.0.1",
    "vue-resource": "^1.0.3"
  },
  "dependencies": {
    "jasmine-core": "^2.5.2",
    "karma": "^1.3.0",
    "karma-babel-preprocessor": "^6.0.1",
    "karma-chrome-launcher": "^2.0.0",
    "karma-firefox-launcher": "^1.0.0",
    "karma-jasmine": "^1.0.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-webpack": "^1.8.0"
  }
}

我创建了app7 / api / rest / urls_api.py:

django-admin.py startproject projet7
cd projet7
django-admin.py startapp app7
mkdir -p app7/api/rest
touch app7/api/__init__.py
touch app7/api/rest/__init__.py

我在projet7 / urls.py中添加了这个

from django.conf import urls
urlpatterns = []

def register(view):
    p = urls.url(view.url_regex, view.as_view())
    urlpatterns.append(p)
    return view

有效!大

但如果我添加它,请在projet7 / urls.py中添加:

from django.conf.urls import include
from app7.api.rest import urls_api
...
urlpatterns = [
    ...
    url(r'^api/', include(urls_api)),
]

它不起作用。我收到这个错误。我不懂为什么。它应该是一样的吗?

   from django.conf.urls import include
   from app7.api import rest
   ...
    urlpatterns = [
        ...
        url(r'^api/', include(rest.urls_api)),
    ]

由于

1 个答案:

答案 0 :(得分:1)

嗯,这不一样。

执行from app7.api import rest后,您几乎导入app7/api/rest/__init__.py

os.path可能是您知道此导入样式的地方,并且special magic可以实现所有这些。)

我只是建议导入最具体的模块,或者如果您确定不想这样做,请在import .urls_api as urls_apirest/__init__.py(但要注意最终可能的通知)进口问题)。