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
ed2bc787
Commit
ed2bc787
authored
Jun 29, 2015
by
Petrus J.v.Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding your own views: Intro text. Standalone views.
parent
5c5c62bb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
68 deletions
+21
-68
adding_your_own_views.rst
doc/adding_your_own_views.rst
+21
-68
No files found.
doc/adding_your_own_views.rst
View file @
ed2bc787
Adding your own views
Adding your own views
===========
======================
For situations where your requirements are really specific, and you struggle to meet
them with the builtin :class:`~flask_admin.model.ModelView` class: Flask-Admin makes it really easy for you to
take full control and ass some views yourself.
Standalone views
Standalone views
------------
------------------
Now, lets add an administrative view. The next example will result in two items appearing in the navbar menu: *Home*
To add a set of standalone views, that are not tied to any particular model, you can extend the
and *Hello*. To do this, you need to derive from the :class:`~flask_admin.base.BaseView` class::
:class:`~flask_admin.base.BaseView` class, and define your own view methods on it. For
example, to add a page that displays some analytics data from a 3rd-party API::
from flask import Flask
from flask_admin import BaseView, expose
from flask_admin import Admin, BaseView, expose
class
My
View(BaseView):
class
Analytics
View(BaseView):
@expose('/')
@expose('/')
def index(self):
def index(self):
return self.render('index.html')
return self.render('
analytics_
index.html')
a
pp = Flask(__name__
)
a
dmin.add_view(CustomView(name='Analytics')
)
admin = Admin(app)
This will add a link to the navbar, from where your view can be accessed. Notice that
admin.add_view(MyView(name='Hello'))
it is served at the root URL, '/'. This is a restriction on standalone views: each view class needs
to serve a view at its root.
app.run()
The `analytics_index.html` template for the example above, could look something like::
One important restriction on admin views is that each view class should have a default page-view method with a root
url, '/'. The following example is correct::
class MyView(BaseView):
@expose('/')
def index(self):
return self.render('index.html')
but, this wouldn't work::
class MyView(BaseView):
@expose('/index/')
def index(self):
return self.render('index.html')
Now, create a new *index.html* file with following content::
{% extends 'admin/master.html' %}
{% extends 'admin/master.html' %}
{% block body %}
{% block body %}
Hello World from MyView!
<p>Here I'm going to display some data.</p>
{% endblock %}
{% endblock %}
and place it in a *templates* directory. To maintain a consistent look and feel, all administrative pages should extend
By extending the *admin/master.html* template, you can maintain a consistent user experience,
the *admin/master.html* template.
even while having tight control over your page's content.
You should now see your new admin page in action on the *Hello* page
.. image:: images/quickstart/quickstart_2.png
:width: 640
:target: ../_images/quickstart_2.png
To add another level of menu items, you can specify a value for the *category* parameter when passing admin views to
the Admin instance. The category specifies the name of the top-level menu item, and all of the views that are associated
with it, will be accessible from a drop-down menu. For example::
from flask import Flask
from flask_admin import Admin, BaseView, expose
class MyView(BaseView):
@expose('/')
def index(self):
return self.render('index.html')
app = Flask(__name__)
admin = Admin(app)
admin.add_view(MyView(name='Hello 1', endpoint='test1', category='Test'))
admin.add_view(MyView(name='Hello 2', endpoint='test2', category='Test'))
admin.add_view(MyView(name='Hello 3', endpoint='test3', category='Test'))
app.run()
will look like
.. image:: images/quickstart/quickstart_3.png
:width: 640
:target: ../_images/quickstart_3.png
Overriding the builtin views
Overriding the builtin views
------------------------------------
------------------------------------
If you want most of the builtin ModelView functionality, but you want to have your own view
If you want most of the builtin ModelView functionality, but you want to have your own view
in place of the default `create`, `edit`, or `list` view.
in place of the default `create`, `edit`, or `list` view. Then you can simply override
override the view in question::
Then you can override the view in question as follows::
from flask_admin.contrib.sqla import ModelView
from flask_admin.contrib.sqla import ModelView
...
...
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