尝试使用 django-piston (v 0.2.3) 创建 API 应用程序并在基本 url.py 中包含应用程序命名空间
url(r'^news/', include('news.urls')),
url(r'^api/', include('api.urls')),
我所有的应用程序不再导入模型,我收到以下错误...
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/api/story
Django Version: 1.4.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'django_extensions',
'debug_toolbar',
'polls',
'news',
'api')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
89. response = middleware_method(request)
File "/Library/Python/2.7/site-packages/django/middleware/common.py" in process_request
67. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in is_valid_path
531. resolve(path, urlconf)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
420. return get_resolver(urlconf).resolve(path)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Users/digitalandmedia2/Documents/MYCOMPANY/django_prototype/mysite/urls.py" in <module>
13. url(r'^api/', include('api.urls')),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py" in include
24. urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Users/digitalandmedia2/Documents/MYCOMPANY/django_prototype/api/urls.py" in <module>
5. from api.handlers import StoryHandler
File "/Users/digitalandmedia2/Documents/MYCOMPANY/django_prototype/api/handlers.py" in <module>
3. from news.models import Story
Exception Type: ImportError at /api/story
Exception Value: No module named models
这是 api 应用程序的 url.py ......
from django.conf.urls.defaults import *
from django.conf import settings
from piston.resource import Resource
from api.handlers import StoryHandler
story_handler = Resource(StoryHandler)
#look into grouping urls
urlpatterns = patterns('',
url(r'^story$', story_handler),
)
这是处理程序...
from piston.handler import BaseHandler
from news.models import Story
class StoryHandler(BaseHandler):
allowed_methods = ('GET',)
model = Story
def read(self, request):
"""
Returns a story lineup
"""
return Story.objects.all().order_by('-pub_date')[:5]
目录结构...
manage.py
/admin
...
/api
handlers.py
urls.py
...
/news
models.py
views.py
urls.py
/mysite
settings.py
urls.py
....