0

When trying to create an API app using django-piston (v 0.2.3) and including the app namespace in the base url.py

url(r'^news/', include('news.urls')),
url(r'^api/', include('api.urls')),

All my apps no longer import models and I get the following error...

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

Here is the url.py for api app...

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),
)

Here is the 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]

Directory Structure...

manage.py
/admin
  ...
/api
  handlers.py
  urls.py
  ...
/news
   models.py
   views.py
   urls.py
/mysite
  settings.py
  urls.py
  ....
a11hard
  • 1,904
  • 4
  • 19
  • 41
  • Dumb question first: Does `news/models.py` exist? – Jamey Sharp Nov 01 '12 at 20:11
  • yes it is in the news app which works fine when I don't include the api namespace in the base url.py I added an edit with directory structure – a11hard Nov 01 '12 at 20:12
  • Thanks. Does `api/` have a module named `news` in it? That would be found before the top-level `news` module. – Jamey Sharp Nov 01 '12 at 20:14
  • Ah, yes it did. It was empty but going to be used to break up handlers. I removed it and the error persists – a11hard Nov 01 '12 at 20:16
  • I wouldn't trust the auto-reloading in the development server for this; did you try outright killing the development server and re-starting it? – Jamey Sharp Nov 01 '12 at 20:18
  • I killed it and restarted, no change. Just to confirm b/c I am new at this you mean kill the server and restart with "python manage.py runserver" – a11hard Nov 01 '12 at 20:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18938/discussion-between-adgezaza-and-jamey-sharp) – a11hard Nov 01 '12 at 20:23
  • looks like there is a conflict with the name news when using piston. I have to confirm this – a11hard Nov 01 '12 at 20:38

0 Answers0