Django REST framework is a powerful and flexible toolkit for building Web APIs. It provides a full suite of features, including authentication, authorization, and request throttling. One of the most popular authentication methods is Basic HTTP Authentication. In this tutorial, we will show you how to use Basic HTTP Authentication in Django REST framework.
The first step is to install the Django REST framework. You can do this using pip:
pip install djangorestframework
Once the installation is complete, you can verify that the framework is installed by running the following command:
python -m django --version
If the installation was successful, you should see the version of the Django REST framework that you installed.
The next step is to create a Django project. To do this, you can use the django-admin
command:
django-admin startproject myproject
This will create a new directory called myproject
with the necessary files and directories for a Django project. You can then change into the new directory and start the development server:
cd myproject
python manage.py runserver
If the server starts successfully, you should see a message like this:
Starting development server at http://127.0.0.1:8000/
You can now open a web browser and navigate to http://127.0.0.1:8000/ to see the default Django page.
Now that you have a Django project, you can add the Django REST framework to it. To do this, open the settings.py
file in the project directory and add the following line to the INSTALLED_APPS
list:
'rest_framework',
This will enable the Django REST framework in your project. You can then run the following command to apply the changes:
python manage.py migrate
This will create the necessary database tables for the Django REST framework.
The next step is to configure the authentication classes. To do this, open the settings.py
file and add the following line to the REST_FRAMEWORK
dictionary:
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.BasicAuthentication',),
This will enable Basic HTTP Authentication for the Django REST framework.
Now that the authentication classes are configured, you can create a view that requires authentication. To do this, open the views.py
file in the project directory and add the following code:
from rest_framework.decorators import authentication_classes
from rest_framework.response import Response
@authentication_classes(['rest_framework.authentication.BasicAuthentication'])
def my_view(request):
return Response({'message': 'Hello, World!'})
This view requires authentication using Basic HTTP Authentication. You can then add a URL pattern to the urls.py
file to map the view to a URL:
from django.urls import path
from .views import my_view
urlpatterns = [
path('my-view/', my_view),
]
This will map the view to the URL http://127.0.0.1:8000/my-view/.
The final step is to test the view. To do this, open a web browser and navigate to the URL http://127.0.0.1:8000/my-view/. You should see a prompt for a username and password. Enter a valid username and password and you should see the message “Hello, World!”.
Congratulations! You have successfully configured Basic HTTP Authentication in Django REST framework.