Quick Start Tutorial: NameError: name 'viewsets' is not defined

1,565 views
Skip to first unread message

Adrian Lazăr

unread,
Dec 22, 2018, 5:38:21 PM12/22/18
to Django REST framework
Hello guys, 

I am struggling to determine what seems to be the issue here. There error that I get is: NameError: name 'viewsets' is not defined

I am following the Quick Start Tutorial while using Python3 and Django 2.1

Here is my views.py code:
from django.shortcuts import render
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from . serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
serializer_class = UserSerializer
queryset = User.objects.all().order_by('-date_joined')


class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
serializer_class = GroupSerializer
queryset = Group.objects.all()
And here is my urls.py code: 

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.


urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]




As you can see, I have imported the viewsets and declared them for the class argument. Please let me know what am I doing wrong. Here is the link for the short tutorial: https://www.django-rest-framework.org/tutorial/quickstart/

Thank you!! 





























Roberth Mejias

unread,
Dec 22, 2018, 7:41:04 PM12/22/18
to django-res...@googlegroups.com
Did you add rest_framework in the settings.py file? in the definition of applications?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
piedrucci...

Adrian Lazăr

unread,
Dec 22, 2018, 8:44:08 PM12/22/18
to django-res...@googlegroups.com
Yes. It is listed within the installed_apps list. 

Wanderley S

unread,
Dec 22, 2018, 10:21:50 PM12/22/18
to django-res...@googlegroups.com
Hi, I believe the issue is at the import: "from . serializers import...." 

Try to remove the space between the dot and the serializers:

"from .serializers import ..."

Regards

Adrian Lazăr

unread,
Dec 23, 2018, 11:41:06 AM12/23/18
to Django REST framework
Hello, The space between the dot and the module does not make any difference, thank you though. 

I have managed to resolve this issue by re-writing the entire project. Somehow it was bugged from the beginning. Thank you guys!
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
piedrucci...

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Gonzalo Amadio

unread,
Dec 23, 2018, 7:58:49 PM12/23/18
to Django REST framework
When you have a problem like this, try first to remove all .pyc files 

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
piedrucci...

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
--------
Gonzalo Amadio
Reply all
Reply to author
Forward
0 new messages