Django Rest Framework Basic Authentication

Adi Ramadhan
2 min readJul 18, 2020

--

Create DRF REST API with Basic Authentication.
Official docs here.

Step 1: Preparation, Create Django Project, Initial Migration
Install virtualenv: pip install virtualenv
Create virtualenv: virtualenv venv
Start virtualenv: venv/Scripts/activate
Install Django, DRF in virtualenv: pip install django djangorestframework
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate
Create superuser: python manage.py createsuperuser
(for example: username=admin)

Step 2: Create App
Create app: python manage.py createapp myapp
Create API in myapp/views.py

myapp/views.py

Step 3: Set up urls
Create myapp/urls.py

myapp/urls.py

Update myproject/urls.py

myproject/urls.py

Step 4: Settings.py
Register myapp and rest_framework in INSTALLED_APPS

INSTALLED_APPS in myproject/settings.py

Step 6: Testing
Run server: python manage.py runserver
Test call API using POSTMAN. Download and see official docs here.
http://127.0.0.1:8000/hello/ with Basic Auth with user that created before (see Step 1),

--

--

Adi Ramadhan
Adi Ramadhan

No responses yet