Commit a3fac71e authored by Petrus J.v.Rensburg's avatar Petrus J.v.Rensburg

Customising Builtin Views: overriding the default templates.

parent b7ae674a
...@@ -22,6 +22,7 @@ Examples ...@@ -22,6 +22,7 @@ Examples
Flask-Admin comes with several examples that will help you get a grip on what's possible. Flask-Admin comes with several examples that will help you get a grip on what's possible.
Have a look at http://examples.flask-admin.org/ to see them in action, or browse through the `examples` directory in the GitHub repository. Have a look at http://examples.flask-admin.org/ to see them in action, or browse through the `examples` directory in the GitHub repository.
****
Getting Started Getting Started
================= =================
...@@ -90,6 +91,7 @@ is that it's just an empty page with a navigation menu. To add some content to t ...@@ -90,6 +91,7 @@ is that it's just an empty page with a navigation menu. To add some content to t
This will override the builtin index template, but still give you the builtin navigation menu. So, now you can add any content to the index page that makes sense for your app. This will override the builtin index template, but still give you the builtin navigation menu. So, now you can add any content to the index page that makes sense for your app.
****
Authorisation & Permissions Authorisation & Permissions
================================= =================================
...@@ -162,6 +164,7 @@ https://github.com/flask-admin/Flask-Admin/tree/master/examples/auth. ...@@ -162,6 +164,7 @@ https://github.com/flask-admin/Flask-Admin/tree/master/examples/auth.
The example only uses the builtin `register` and `login` views, but you could follow the same The example only uses the builtin `register` and `login` views, but you could follow the same
approach for including the other views, like `forgot_password`, `send_confirmation`, etc. approach for including the other views, like `forgot_password`, `send_confirmation`, etc.
****
.. _customising-builtin-views: .. _customising-builtin-views:
...@@ -281,60 +284,23 @@ related models loaded via ajax, using:: ...@@ -281,60 +284,23 @@ related models loaded via ajax, using::
Overriding the default templates Overriding the default templates
--------------------------------- ---------------------------------
To do this, find your flask-admin installation (this could be somewhere like `/env/lib/python2.7/site-packages/flask_admin/` You can override any of the builtin Flask-Admin templates by simply copying them
and copy the template in `templates/bootstrap3/admin/index.html` to your own project directory at `my_app/templates/admin/index.html`. into `templates/admin/` in your project directory. This gives you absolute
control over the look & feel of the admin interface, but with one drawback: it
can make life difficult for you when you eventually want to upgrade the version
of Flask-Admin that you are using.
If you want to keep your custom templates in some other location, then you need
to remember to reference them from the ModelView classes where you intend to
use them, e.g.::
class BaseModelView(ModelView):
list_template = 'base_list.html'
create_template = 'base_create.html'
edit_template = 'base_edit.html'
To customize these model views, you have two options: Either you can override the public properties of the *ModelView* Have a look at the `layout` example at https://github.com/flask-admin/flask-admin/tree/master/examples/layout
class, or you can override its methods. if you want to see how to take full stylistic control.
For example, if you want to disable model creation and only show certain columns in the list view, you can do
something like::
from flask_admin.contrib.sqla import ModelView
# Flask and Flask-SQLAlchemy initialization here
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('login', 'email')
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(User, session, **kwargs)
admin = Admin(app)
admin.add_view(MyView(db.session))
Overriding form elements can be a bit trickier, but it is still possible. Here's an example of
how to set up a form that includes a column named *status* that allows only predefined values and
therefore should use a *SelectField*::
from wtforms.fields import SelectField
class MyView(ModelView):
form_overrides = {
'status': SelectField
}
form_args = {
# Pass the choices to the `SelectField`
'status': {
choices=[(0, 'waiting'), (1, 'in_progress'), (2, 'finished')]
}
}
It is relatively easy to add support for different database backends (Mongo, etc) by inheriting from
:class:`~flask_admin.model.BaseModelView`.
class and implementing database-related methods.
Please refer to :mod:`flask_admin.contrib.sqla` documentation on how to customize the behavior of model-based
administrative views.
Replacing specific form fields Replacing specific form fields
------------------------------------------ ------------------------------------------
...@@ -391,6 +357,8 @@ You'll need to specify an upload directory, and then use either `FileUploadField ...@@ -391,6 +357,8 @@ You'll need to specify an upload directory, and then use either `FileUploadField
If you just want to manage static files, without tying them to a database model, then If you just want to manage static files, without tying them to a database model, then
rather use the :ref:`File-Admin<file-admin>` plugin. rather use the :ref:`File-Admin<file-admin>` plugin.
****
Adding your own views Adding your own views
====================== ======================
For situations where your requirements are really specific, and you struggle to meet For situations where your requirements are really specific, and you struggle to meet
...@@ -447,8 +415,7 @@ override the view in question, and all the links to it will still function as yo ...@@ -447,8 +415,7 @@ override the view in question, and all the links to it will still function as yo
return self.render('create_user.html') return self.render('create_user.html')
****
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2
...@@ -456,11 +423,14 @@ override the view in question, and all the links to it will still function as yo ...@@ -456,11 +423,14 @@ override the view in question, and all the links to it will still function as yo
advanced advanced
api/index api/index
****
Support Support
---------- ----------
Python 2.6 - 2.7 and 3.3 - 3.4. Python 2.6 - 2.7 and 3.3 - 3.4.
****
Indices and tables Indices and tables
------------------ ------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment