Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
flask-admin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
flask-admin
Commits
d30a50f4
Commit
d30a50f4
authored
Jul 02, 2015
by
Petrus J.v.Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Advanced: Localization with Flask-Babelex. Add 'tips' and 'templates' pages to index.
parent
c943e859
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
60 deletions
+29
-60
advanced.rst
doc/advanced.rst
+25
-58
index.rst
doc/index.rst
+4
-2
No files found.
doc/advanced.rst
View file @
d30a50f4
...
...
@@ -10,26 +10,17 @@ As an alternative to passing a Flask application object to the Admin constructor
# Add views here
admin.init_app(app)
Enabling localization
----------------------
Flask-Admin makes it possible for you to serve your application in more than one language. To do this, it makes use of
the `Flask-BabelEx <http://github.com/flask-admin/flask-babelex/>`_ package for handling translations. This package is a
fork of the popular `Flask-Babel <http://github.com/mitshuhiko/flask-babel/>`_ package, with the following features:
Localization with Flask-Babelex
------------------------------------------
Enabling localization is relatively simple.
1. It is API-compatible with Flask-Babel
2. It allows distribution of translations with Flask extensions
3. It aims to be more configurable than Flask-Babel
Currently *Flask-BabelEx* is the only supported way of enabling localization support in Flask-Admin.
How to enable localization
^^^^^^^^^^^^^^^^^^
1. Install Flask-BabelEx::
#. Install `Flask-BabelEx <http://github.com/mrjoes/flask-babelex/>`_ to do the heavy
lifting. It's a fork of the
`Flask-Babel <http://github.com/mitshuhiko/flask-babel/>`_ package::
pip install flask-babelex
2
. Initialize Flask-BabelEx by creating instance of `Babel` class::
#
. Initialize Flask-BabelEx by creating instance of `Babel` class::
from flask import app
from flask_babelex import Babel
...
...
@@ -37,20 +28,24 @@ How to enable localization
app = Flask(__name__)
babel = Babel(app)
3
. Create a locale selector function::
#
. Create a locale selector function::
@babel.localeselector
def get_locale():
# Put your logic here. Application can store locale in
# user profile, cookie, session, etc.
return 'en'
if request.args.get('lang'):
session['lang'] = request.args.get('lang')
return session.get('lang', 'en')
So, you could try out a French version of the application at: `http://localhost:5000/admin/?lang=fr <http://localhost:5000/admin/?lang=fr>`_.
4. Initialize Flask-Admin as usual.
#. Add your own logic to the locale selector function:
You can check the `babel` example to see localization in action. When running this example, you can change the
locale simply by adding a query parameter, like *?en=<locale name>* to the URL. For example, a French version of
the application should be accessible at:
`http://localhost:5000/admin/userview/?lang=fr <http://localhost:5000/admin/userview/?lang=fr>`_.
The application could store locale in
a user profile, cookie, session, etc. And it could interrogate the `Accept-Language`
header for making the selection automatically.
If the builtin translations are not enough, look at the `Flask-BabelEx documentation <https://pythonhosted.org/Flask-BabelEx/>`_
to see how you can add your own.
Handling Foreign Key relations inline
--------------------------------------------
...
...
@@ -525,18 +520,6 @@ You might want to check :doc:`api/mod_model` for basic model configuration optio
backends) and specific backend documentation, for example :doc:`api/mod_contrib_sqla`. There's much more
than what is displayed in this table.
Authentication
**************
To restrict access to your admin interface, you can implement your own class for creating admin components, and
override the `is_accessible` method::
class MyModelView(ModelView):
def is_accessible(self):
return login.current_user.is_authenticated()
Components that are not accessible to a particular user, will also not be displayed in the menu for that user.
Templates
***********
...
...
@@ -559,21 +542,5 @@ and then point your class to this new template::
For list of available template blocks, check :doc:`templates`.
Tips and hints
***************************
1. Programming with Flask-Admin is not very different from normal application development - write some views and expose
them to the user, using templates to create a consistent user experience.
2. If you are missing some functionality which can be used more than once, you can create your own "base" class and use
it instead of default implementation.
3. Using Jinja2, you can easily extend the existing templates. You can even change the look and feel of the admin
interface completely, if you want to. Check `this example <https://github.com/flask-admin/flask-admin/tree/master/examples/layout>`_.
4. You are not limited to a simple CRUD interface for every model. Want to add some kind of realtime monitoring via websockets? No problem.
5. There's a so called "index view". By default it is empty, but you can put any information you need there. It is displayed
under the *Home* menu option.
doc/index.rst
View file @
d30a50f4
...
...
@@ -121,7 +121,8 @@ could be as simple as::
def is_accessible(self):
return login.current_user.is_authenticated()
However, you would still need to implement all of the relevant login,
Components that are not accessible to a particular user, will also not be displayed
in the menu for that user. But, you would still need to implement all of the relevant login,
registration and account management views yourself.
For a basic example of this, have a look at https://github.com/flask-admin/Flask-Admin/tree/master/examples/auth-flask-login.
...
...
@@ -401,7 +402,6 @@ even while having tight control over your page's content.
Overriding the Builtin Views
------------------------------------
There may be some scenarios where you want most of the builtin ModelView
functionality, but you want to replace one of the default `create`, `edit`, or `list` views.
For this you could override only the view in question, and all the links to it will still function as you would expect::
...
...
@@ -425,6 +425,8 @@ For this you could override only the view in question, and all the links to it w
.. toctree::
:maxdepth: 2
tips
templates
advanced
api/index
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment