How to Install Django Rest API Framework
Installation of django REST API

# Create folder "my_app"
$ mkdir my_app
$ cd my_app

# Creating virtualenv inside "my_app"
$ py -m virtualenv env

# Activate virtualenv
$ .\env\Scripts\activate

# Installation of Basic packages/Requirements

$ (env) pip install requests
$ (env) pip install mysqlclient
$ (env) pip install djangorestframework
$ (env) pip install django

# Create Project Setup
$ (env) pip startproject tutorial .
$ (env) cd tutorial
$ (env) django-admin startapp quickstart
$ (env) cd ..

# Mysql Setup
1. Update tutorias/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_app',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': ''
}
}

2. Create DB name "my_app"

$ (env) python manage.py migrate

# Check list of tables created

3. Add the following tutorials/settings.py inside "INSTALLED_APPS = ["

'rest_framework',
'rest_framework.authtoken',

4. Include Token Authentication tutorials/settings.py
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 2,
    'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated', )
}

$ (env) python manage.py migrate

# Check again the list of tables created

5. Create username and password

$ (env) python manage.py createsuperuser --email admin@example.com --username admin

6. Run the instance
$ (env) python manage.py runserver

7. Generate List of libraries /packages installed to requirements.txt
$ pip freeze > requirements.txt

8. Install libraries /packages from requirements
$ pip install -r requirements.txt

Tags:

Comments are closed.