Loader użytkownika i UserMixin
Skonfiguruj LoginManager i model użytkownika.
Loader użytkownika i UserMixin to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 👤
Często zadawane pytania
Czy lekcja „Loader użytkownika i UserMixin” jest bezpłatna?
Tak — pełny tekst „Loader użytkownika i UserMixin” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Loader użytkownika i UserMixin”?
Skonfiguruj LoginManager i model użytkownika. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Flask Academy?
Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Loader użytkownika i UserMixin”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?
Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Haszowanie haseł — nigdy nie przechowuj tekstu jawnego
- Loader użytkownika i UserMixin
- login_user, logout_user i sesje
- Ochrona widoków za pomocą login_required