Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 72 · Structure and Configuration

Flask-Admin and Flask-Babel

Add a database admin panel with Flask-Admin ModelView and lock it down, then translate your app and format dates, numbers and currencies for each locale with Flask-Babel.

Two chores appear in almost every growing web app. Someone who is not a developer needs to edit the data: add a product, fix a typo, hide a post. And visitors from other countries expect the site in their language and format: 5 de marzo de 2026, 1.234,50 €. Two extensions solve these:

  • Flask-Admin generates a ready-made admin interface (list, search, filter, create, edit, delete) for your SQLAlchemy models.
  • Flask-Babel adds translations and locale-aware formatting based on the Babel library.

They are independent, so this lesson has two halves. Both install with pip install flask-admin flask-babel and both run in the editors below.

Flask-Admin: an admin panel in a few lines

You create an Admin object, then register one ModelView per model. ModelView builds list pages with sorting, a search box, filters and pagination, plus forms to create and edit rows, from your model's columns. The panel lives under /admin/ by default.

Securing it is your job

Flask-Admin has no login of its own. An unprotected ModelView lets anyone on the internet read and delete your data. Always override is_accessible() (who may use the view) and inaccessible_callback() (where to send everyone else), and do the same for the admin home page. Use the real authentication from Flask-Login and the roles from Authorization in production.

The example puts both checks into small base classes and uses a session flag as a stand-in for a real logged-in admin user. It then drives the admin with the test client, so you can see exactly what a visitor and an administrator get.

Python A secured Flask-Admin panel

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The list page rendered for the signed-in user and redirected the visitor. Useful options on a view:

  • column_list, column_exclude_list: which columns appear in the table.
  • column_searchable_list, column_filters, column_sortable_list: search box, filters and sortable columns.
  • form_columns, form_excluded_columns: which fields the forms show. Keep sensitive fields such as password_hash out of the forms entirely.
  • can_create, can_edit, can_delete, can_export: switch actions on or off.
  • column_formatters and form_args: customize display and form-field options.

Flask-Admin forms are built with WTForms, so they bring their own CSRF protection when you use its SecureForm base. Test the panel like any other part of the app (see Testing Flask Apps). Many teams use it only for internal tools; a public-facing management interface deserves a purpose-built UI.

Flask-Babel: translations and locale formatting

Internationalization (i18n) prepares an app for several languages; localization (l10n) is adding one. Flask-Babel supports both:

  1. Wrap user-visible strings in gettext (usually imported as _) in Python and {{ _("text") }} in templates.
  2. Extract those strings into a template file, translate them into .po files per language, and compile them to .mo files with the pybabel command.
  3. Tell Flask-Babel how to pick the locale for each request, for example from the Accept-Language header, a user setting, a cookie or a URL prefix.
flowchart LR
    A["_('Hello')<br/>in code and templates"] -- "pybabel extract" --> B["messages.pot"]
    B -- "pybabel init -l es" --> C["es/messages.po<br/>(translators edit)"]
    C -- "pybabel compile" --> D["es/messages.mo"]
    D --> E["Flask-Babel looks it up<br/>at request time"]

Run pybabel extract, update, compile as part of your workflow; translators only touch the .po files.

The commands (read-only, on your machine):

TEXT
pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d translations -l es
pybabel compile -d translations
pybabel update -i messages.pot -d translations     # after you add strings later

In the browser example we skip the command line: we build the Spanish catalog in code with Babel's own API, which produces the same .mo file pybabel compile would, and let Flask-Babel find it.

Python gettext, plurals and locale-aware formatting

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What to observe:

  • Spanish gets translated text, plural forms (artículo versus artículos chosen by ngettext from the number), a long date in Spanish word order, a euro amount written 1.234,50 €, and a number with Spanish separators.
  • English uses the untranslated original strings, which also serve as the keys. That is why the source text must be stable and complete: write whole sentences, never glue translated fragments together.
  • fr is not in the list passed to best_match, so it falls back to the first entry (en). A language with no catalog shows the original text.

Using it in templates and forms

In Jinja, {{ _("Welcome") }} translates, {{ _("Hello %(name)s", name=user.name) }} interpolates safely, and filters such as {{ price|currencyformat("EUR") }} and {{ when|datetimeformat }} (Flask-Babel registers them) format values for the active locale. Strings that are defined at import time, such as form labels or model column names, must use lazy_gettext so they are translated when displayed rather than when the module loads.

Choosing the locale well

The locale_selector function runs on each request and may return any locale code. Common strategies, from most to least explicit: a URL prefix (/es/...) which is also good for SEO, a saved user preference, a language cookie, then the Accept-Language header as a last resort. Also set a timezone_selector if you show times, and store all times in UTC. Keep Jinja filters for the custom formatting you still need.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What must you add yourself to a Flask-Admin ModelView before using it on a real site?

    Flask-Admin has no built-in authentication. Without is_accessible checks, anyone who knows /admin/ can read and change the data.
  2. 2. Which option controls which fields appear in the create and edit forms?

    column_list affects the table. form_columns (or form_excluded_columns) decides which fields the forms show, which is how you keep password hashes out.
  3. 3. What does ngettext do?

    ngettext(singular, plural, n) picks the correct plural form according to the active language's rules.
  4. 4. Why use lazy_gettext for a form label defined at module level?

    gettext needs the current request's locale. lazy_gettext returns a proxy that is translated only when it is rendered.

Summary

  • Flask-Admin turns models into an admin UI with Admin(app) and ModelView, but you must secure it yourself with is_accessible and inaccessible_callback.
  • Tune views with column_list, column_searchable_list, column_filters, form_columns and the can_* flags.
  • Flask-Babel wraps strings in gettext/_, plurals in ngettext, and uses .po/.mo files produced with pybabel extract, init, update and compile.
  • A locale_selector decides the language per request; Babel also formats dates, numbers and currencies for the locale.
  • Use lazy_gettext for strings created at import time, and write whole translatable sentences.

Next: let users sign in with an existing account in Social Login with OAuth (Authlib).