Django REST Framework का परिचय
DRF किस Problem को Solve करता है?
एक plain Django view HTML return करता है। कई apps (mobile apps, JavaScript frontends, दूसरी services) को इसके बजाय data JSON के रूप में चाहिए होता है। DRF आपको अपने models को एक clean JSON API के रूप में expose करने के tools देता है बिना JSON parsing और validation logic हाथ से लिखे।
उदाहरण: What Problem Does DRF Solve?
A plain Django view returns HTML. Many apps (mobile apps, JavaScript frontends, other services) instead need data as JSON. DRF gives you the tools to expose your models as a clean JSON API without writing JSON parsing and validation logic by hand.
# A plain Django view returns HTML for humans:
def book_list(request):
books = Book.objects.all()
return render(request, 'books/list.html', {'books': books})
# DRF instead returns JSON for programs to consume,
# built from the same Book model using a serializer + API view.
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
DRF Django पर Built है
DRF Django के models, URLs, या settings को replace नहीं करता — यह नए pieces (serializers और API views) add करता है जो उनके साथ काम करते हैं। आपका पहले वाले chapters का Book model DRF के साथ सीधे reuse हो सकता है।
उदाहरण: DRF Is Built on Django
DRF does not replace Django's models, URLs, or settings — it adds new pieces (serializers and API views) that work alongside them. Your Book model from earlier chapters can be reused directly with DRF.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_year = models.IntegerField()
def __str__(self):
return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
The Browsable API
DRF की सबसे useful features में से एक browsable API है: एक API endpoint को एक normal web browser में visit करना raw text के बजाय JSON data और requests try करने के लिए forms के साथ एक readable HTML page दिखाता है।
- Browsable API सिर्फ browsers को दिखाई जाती है।
- दूसरे clients (जैसे curl या एक mobile app) को अब भी plain JSON मिलता है।
उदाहरण: The Browsable API
One of DRF's most useful features is the browsable API: visiting an API endpoint in a normal web browser shows a readable HTML page with the JSON data and forms to try requests, instead of raw text.
# No code needed to see this -- once a DRF view is wired up,
# visiting its URL in a browser (e.g. /api/books/) shows:
# - the JSON response
# - a form to POST new data
# - clickable links to related endpoints
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- DRF के serializers और views के बजाय
HttpResponse(json.dumps(...))हाथ से लिखकर एक JSON API बनाने की कोशिश करना, जबकि वे validation, content negotiation, और errors आपके लिए handle करते हैं। - यह मान लेना कि DRF Django से एक separate framework है, जब यह असल में एक library है जो आपके existing Django project और models में plug होती है।
INSTALLED_APPSमें rest_framework entry skip करना और फिर confused होना जब DRF की browsable API या generic views काम न करें।
- Django REST Framework (DRF) Web APIs बनाने के लिए Django के ऊपर बनी एक library है।
- यह आपके existing Django models reuse करता है और serializers, API views, और browsable HTML output add करता है।
- DRF को separately install किया जाना चाहिए और उपयोग किए जाने से पहले
INSTALLED_APPSमें add किया जाना चाहिए।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: