Introduction
DRF helps you build web APIs easily with Django. It makes sharing data between your app and others simple.
Jump into concepts and practice - no test required
DRF helps you build web APIs easily with Django. It makes sharing data between your app and others simple.
pip install djangorestframework # Then add 'rest_framework' to INSTALLED_APPS in settings.py INSTALLED_APPS = [ ... 'rest_framework', ]
pip install djangorestframework
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'rest_framework',
]This example shows how to install DRF, add it to your project, create a simple API view that returns a message, and connect it to a URL.
# 1. Install DRF # Run in terminal: # pip install djangorestframework # 2. Add to settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'rest_framework', ] # 3. Create a simple API view in views.py from rest_framework.views import APIView from rest_framework.response import Response class HelloWorld(APIView): def get(self, request): return Response({"message": "Hello, world!"}) # 4. Add URL in urls.py from django.urls import path from .views import HelloWorld urlpatterns = [ path('hello/', HelloWorld.as_view()), ] # Now, running the server and visiting /hello/ returns JSON with message.
Make sure to restart your Django server after installing and updating settings.
You can test your API by visiting the URL in a browser or using tools like curl or Postman.
Install DRF using pip and add it to your Django settings.
Create API views by subclassing DRF classes like APIView.
Connect views to URLs to make your API accessible.
INSTALLED_APPS list in settings.py.'rest_framework' here enables Django to recognize DRF features.urls.py snippet, what does it enable?from django.urls import path, include
urlpatterns = [
path('api-auth/', include('rest_framework.urls')),
]rest_framework.urls include login and logout views for the browsable API.rest_framework to INSTALLED_APPS but get an error: ModuleNotFoundError: No module named 'rest_framework'. What is the likely cause?ModuleNotFoundError means Python cannot find the DRF package installed.pip install djangorestframework.INSTALLED_APPS. Which is the correct way to update your project's urls.py to achieve this?rest_framework.urls for login/logout views.path('api-auth/', include('rest_framework.urls')) to enable these pages.