Professional Documents
Culture Documents
Submitted By
Sarthak Goyal
Enrolment No.: R110218138
SAP ID: 500067706
Semester VI
B. Tech. (Computer Sc. and Engineering)
Specialization in CCVT
Submitted To
Ms. Shelly
Experiment 4:
Aim: Using Cloud Platform(Django) API
Requirements: Python(2.7.x or 3.4.x), virtualenv, Django, Any IDE(Microsoft Visual Code,
Atom), easy_install, pip.
We are going to create a simple API using which admin users can view and edit the users and
groups in the system.
Step 1: Setup Django Project
Go to the virtual environment cloudapp created in the Experiment 2 & run the following
command to install Django REST Framework into the virtual environment.
Step 2: Create a new project named tutorial using the following command
2
Roll No: R110218138 CAD LAB
SAP ID: 500067706
Step 3: Sync your database for the first time using the following command
Step 4: Create an initial user named admin with password password@123 using the
following command
Step 5: Create another user named sgoyalp1 with password sarthak2019 using the following
command
3
Roll No: R110218138 CAD LAB
SAP ID: 500067706
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'name']
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
4
Roll No: R110218138 CAD LAB
SAP ID: 500067706
class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all()
serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated]
Step 8: Define API URLs. In tutorial/urls.py put the following python code
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
5
Roll No: R110218138 CAD LAB
SAP ID: 500067706
Step 9: Define Pagination. allows you to control how many objects per page are returned. To
enable it add the following lines to tutorial/settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination
',
'PAGE_SIZE': 10
}
6
Roll No: R110218138 CAD LAB
SAP ID: 500067706
7
Roll No: R110218138 CAD LAB
SAP ID: 500067706
These admin users can add more users, we will add a username user1 with email address
user1@example.com
8
Roll No: R110218138 CAD LAB
SAP ID: 500067706
END OF EXERCISE