Error implementing Django REST Framework

0

I have the project on the production server, and I'm passing certain scripts from a local project to the server. And it gives me an error that I can not isolate.

ImportError at /
No module named viewsets
Request Method: GET
Request URL:    http://sub.domain.com/
Django Version: 1.9.6
Exception Type: ImportError
Exception Value:    
No module named viewsets
Exception Location: /home/django/django_project/django_project/urls.py in <module>, line 7
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/home/django/django_project',
 '/home/django',
 '/usr/bin',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
Server time:    Mié, 11 May 2016 13:12:07 +0200

I have the imported viewsets and I do not understand. Since in the other project the API worked perfectly for me.

Here I leave the urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
from rest_framework import routers, serializers, viewsets

# API Imports
from .viewsets import BrandViewSet


admin.autodiscover()


# Routers API
router = routers.DefaultRouter()
router.register(r'brand', BrandViewSet)


urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'app.views.home', name='home'),
    # API Rest Framework
    url(r'^API', include(router.urls)),
    url(r'^admin/', include(admin.site.urls)),
)

PROJECT STRUCTURE:

/home/django/

|--django_project/
   |--django_project/ 
   |  |--settings.py
   |  |--urls.py
   |  |--wsgi.py
   |--my_app/
   |  |--admin.py
   |  |--models.py
   |  |--serializers.py
   |  |--tests.py
   |  |--views.py
   |  |--viewsets.py
   |--manage.py
    
asked by RuralGalaxy 11.05.2016 в 13:56
source

2 answers

2

Since your urls.py is outside of my_app , the relative import does not work, try doing this:

# API Imports
from my_app.viewsets import BrandViewSet

Remember that the relative import works only within the same module you are in.

For more information on the subject of imports you can visit the following link:

answered by 11.05.2016 / 15:15
source
0

Let's see: the error says that "can not find the module viewsets on line 7" , which is this:

from .viewsets import BrandViewSet

You have a . in front of viewsets , which indicates a relative import. I do not think that this module is in the same directory as urls.py , so remove the point to see if it goes.

    
answered by 11.05.2016 в 14:30