كتابة View قائمة على دالة
أنشئ View تُرجع استجابة
كتابة View قائمة على دالة درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What a View Really Is
A Django view is just a Python function that takes a request and returns a response. That is the whole contract. 🙂
The First Argument: request
Every view's first parameter is the request object. Django hands it to you automatically when a URL matches your view.
Importing HttpResponse
To send something back, you return an HttpResponse. Import it from django.http before you use it.
from django.http import HttpResponseYour First View
Here is a complete, working view. It takes request and returns plain text in a response.
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello, Django!')Where Views Live
By convention, views go in your app's views.py file. Keeping them there makes them easy to find and wire up later.
You Must Return Something
A view that returns None raises an error. Django expects an HttpResponse every single time, with no exceptions.
Returning HTML
The response body is just a string, so you can put HTML inside it. The browser renders it like any page.
def home(request):
return HttpResponse('<h1>Welcome</h1>')Views Are Plain Functions
A view is normal Python, so you can run logic inside it: build a message, check a condition, then return your response.
def greet(request):
name = 'friend'
return HttpResponse('Hi ' + name)Naming Your View
Name views after what they do, like home or about. Clear names make your URL file readable when you connect them.
Function-Based vs Class-Based
This style is a function-based view, the simplest kind. Class-based views come later; functions are perfect to start with.
Nothing Runs Until It Is Mapped
A view does nothing on its own. It only runs when a URL points to it, which is the very next step you will learn.
Quick Check
Let's confirm the core contract of a Django view.
Recap: You Wrote a View
A view is a function that takes request and returns an HttpResponse. You import it, write your logic, and live in views.py. 🎉
الأسئلة الشائعة
هل درس «كتابة View قائمة على دالة» مجاني؟
نعم — نص درس «كتابة View قائمة على دالة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «كتابة View قائمة على دالة»؟
أنشئ View تُرجع استجابة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «كتابة View قائمة على دالة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كتابة View قائمة على دالة
- أنماط URL باستخدام path()
- التقاط معاملات URL
- تضمين عناوين URL للتطبيقات وتسمية المسارات