You are on page 1of 9

RECORD OF EXPERIMENTS

Cloud Application Development Lab

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

School of Computer Science


UNIVERSITY OF PETROLEUM AND ENERGY STUDIES
Dehradun-248007
2021-22
Roll No: R110218138 CAD LAB
SAP ID: 500067706

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.

pip install djangorestframework

Step 2: Create a new project named tutorial using the following command

django-admin startproject tutorial


cd tutorial
django-admin startapp quickstart

2
Roll No: R110218138 CAD LAB
SAP ID: 500067706
Step 3: Sync your database for the first time using the following command

python manage.py migrate

Step 4: Create an initial user named admin with password password@123 using the
following command

python manage.py createsuperuser --email admin@example.com --username admin

Step 5: Create another user named sgoyalp1 with password sarthak2019 using the following
command

python manage.py createsuperuser --email sgoyalp1@gmail.com --username sgoyalp1

Step 6: Define serializers. In tutorial/quickstart/serializers.py put the following python


code

3
Roll No: R110218138 CAD LAB
SAP ID: 500067706

from django.contrib.auth.models import User, Group


from rest_framework import serializers

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'name']

Step 7: Define views. In tutorial/quickstart/views.py put the following python code

from django.contrib.auth.models import User, Group


from rest_framework import viewsets
from rest_framework import permissions
from quickstart.serializers import UserSerializer, GroupSerializer

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

from django.urls import include, path


from rest_framework import routers
from quickstart import views

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
}

& add 'rest_framework' to INSTALLED_APPS in tutorial/settings.py

Step 10: Test API using the following command

python manage.py runserver

6
Roll No: R110218138 CAD LAB
SAP ID: 500067706

Log in with user credentials

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

Click Get to view list of users after modifying

8
Roll No: R110218138 CAD LAB
SAP ID: 500067706

END OF EXERCISE

You might also like