APIView وFunction @api_view
إنشاء نقاط النهاية بطريقتين
APIView وFunction @api_view درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Ways to Build Endpoints
DRF lets you write endpoints as classes with APIView or as plain functions decorated with @api_view. Both speak the same JSON dialect.
The Class Style
APIView subclasses Django's View but gives you DRF's request parsing, content negotiation, and tidy response handling for free.
from rest_framework.views import APIViewMethods Map to Verbs
In an APIView you write a method per HTTP verb: get for reads, post for creates, and so on, keeping each action clearly separated.
class BookList(APIView):
def get(self, request):
...
def post(self, request):
...Return a Response
Instead of HttpResponse, you return DRF's Response object, which renders to JSON or the browsable API based on the request.
from rest_framework.response import Response
return Response({'ok': True})Read request.data
DRF parses the body for you, so you read posted JSON from request.data as a normal Python dict, no manual decoding needed.
title = request.data['title']The Function Style
For simple endpoints the @api_view decorator turns an ordinary function into a DRF view, listing which HTTP methods it accepts.
from rest_framework.decorators import api_viewDeclare Allowed Methods
You pass the verbs to the decorator. Any method not listed gets an automatic 405 Method Not Allowed, so you do not handle it yourself.
@api_view(['GET', 'POST'])
def books(request):
...Same Response Object
Function views use the very same DRF Response, so you can mix class and function styles in one project without friction.
return Response(serializer.data)Which Style to Pick
Reach for @api_view when an endpoint is small and one-off; choose APIView when you want reusable, organized methods on a class.
Set the Status
Both styles let you pass status= to Response so clients get the right HTTP code, like 201 when you create something new.
Response(data, status=201)Same Power, Two Shapes
Whether class or function, you get DRF parsing, responses, and status handling, the difference is mostly structure and personal taste.
Quick Check
What does @api_view do to a plain function?
Recap: Two View Styles
You can build endpoints with class-based APIView or function-based @api_view, both returning a DRF Response. Pick the shape that fits. 🧩
الأسئلة الشائعة
هل درس «APIView وFunction @api_view» مجاني؟
نعم — نص درس «APIView وFunction @api_view» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «APIView وFunction @api_view»؟
إنشاء نقاط النهاية بطريقتين تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «APIView وFunction @api_view»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تثبيت DRF وتهيئته
- Serializers: من النموذج إلى JSON
- APIView وFunction @api_view
- واجهة API القابلة للتصفح ورموز الحالة