إطار الجلسات
تخزين البيانات المرتبطة بزائر واحد
إطار الجلسات درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Sessions Exist
HTTP forgets you between requests. The session framework lets Django remember each visitor across page loads, so a login or a cart can survive. 🍪
What a Session Stores
A session is a small bag of data tied to one visitor. Django keeps it on the server and gives the browser only a key to find it again.
The Session Cookie
Django sets a cookie named sessionid in the browser. That cookie holds just the session key, never your private data, which stays safe on the server.
It Is Already On
The SessionMiddleware ships enabled in every new project, so sessions work out of the box without any extra setup from you.
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
]The Sessions App
The django.contrib.sessions app lives in INSTALLED_APPS and creates a table to hold session data after you run migrate.
Where Data Lives
By default Django stores sessions in the database. Each row holds the key, the encoded data, and an expiry date for that visitor.
Choosing a Backend
You pick storage with SESSION_ENGINE. Cache or cached_db backends keep sessions in memory for far faster reads on busy sites.
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'Accessing the Session
In any view, the session hangs off the request as request.session. It behaves like a normal Python dictionary you can read and write.
def home(request):
request.session['theme'] = 'dark'
return render(request, 'home.html')Sessions Expire
Sessions do not last forever. SESSION_COOKIE_AGE sets the lifetime in seconds, defaulting to two weeks before a visitor is forgotten.
SESSION_COOKIE_AGE = 1209600 # two weeksClearing Old Sessions
Expired rows linger in the database. Run clearsessions on a schedule to delete stale data and keep the table tidy.
python manage.py clearsessionsAnonymous and Logged In
Sessions track both anonymous and logged-in visitors. That is how a guest can fill a cart before they ever create an account.
Quick Check
Let us check where session data actually lives.
Recap
You met the session framework: it remembers visitors across requests, stores data server-side, and works automatically through SessionMiddleware. Next you will read and write it. ✅
الأسئلة الشائعة
هل درس «إطار الجلسات» مجاني؟
نعم — نص درس «إطار الجلسات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «إطار الجلسات»؟
تخزين البيانات المرتبطة بزائر واحد تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إطار الجلسات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.