محمّل المستخدم وUserMixin
اضبط LoginManager ونموذج المستخدم.
محمّل المستخدم وUserMixin درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet Flask-Login
The Flask-Login extension manages who is logged in for you. It tracks the current user across requests so you do not wire sessions by hand.
pip install flask-loginCreate a LoginManager
Everything starts with a LoginManager. You create one instance and connect it to your app so Flask-Login can hook into requests.
from flask_login import LoginManager
login_manager = LoginManager()
login_manager.init_app(app)It Needs Two Things
To work, the manager needs a user model that follows its rules and a function that loads a user by id. Today you set up both.
The UserMixin Shortcut
Your model must expose a few properties like is_authenticated. The UserMixin class supplies them all, so you just inherit from it.
from flask_login import UserMixin
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)What UserMixin Gives You
UserMixin adds is_authenticated, is_active, is_anonymous, and get_id for free. That last one returns your user id as a string.
The user_loader Callback
Flask-Login stores only the user id in the session. It calls your user_loader on each request to turn that id back into a real user.
Register the Loader
Decorate a function with login_manager.user_loader. It receives the id from the session and must return a user object or None.
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))Ids Arrive as Strings
The id passed to your loader is always a string, since it came from a cookie. Cast it to int before querying an integer primary key.
return User.query.get(int(user_id))Return None for Unknowns
If the id matches no row, your loader should return None. Flask-Login then treats the request as coming from an anonymous visitor.
Override get_id If Needed
If your primary key is not named id, override get_id so it returns the right value. Most apps with a plain id column never touch it.
def get_id(self):
return str(self.uuid)Pieces Now Connected
With the manager, the UserMixin model, and a user_loader in place, Flask-Login can identify the current user on every incoming request.
Quick Check
Think about what the session actually stores between requests.
Recap
You wired a LoginManager, mixed UserMixin into your model, and wrote a user_loader. Flask-Login can now rebuild the current user from a session. 👤
الأسئلة الشائعة
هل درس «محمّل المستخدم وUserMixin» مجاني؟
نعم — نص درس «محمّل المستخدم وUserMixin» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «محمّل المستخدم وUserMixin»؟
اضبط LoginManager ونموذج المستخدم. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «محمّل المستخدم وUserMixin»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تجزئة كلمات المرور وعدم تخزينها كنص صريح
- محمّل المستخدم وUserMixin
- login_user وlogout_user والجلسات
- حماية العروض باستخدام login_required