Commit ada08860 authored by Paul Brown's avatar Paul Brown

improve AdminIndexView docs, prevent breaking static files when url='/'

parent 50de6ff0
...@@ -229,9 +229,15 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)): ...@@ -229,9 +229,15 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
if not self.url.startswith('/'): if not self.url.startswith('/'):
self.url = '%s/%s' % (self.admin.url, self.url) self.url = '%s/%s' % (self.admin.url, self.url)
# If we're working from the root of the site, set prefix to None # If we're working from the root of the site, set prefix to None
if self.url == '/': if self.url == '/':
self.url = None self.url = None
# prevent admin static files from conflicting with flask static files
if not self.static_url_path:
self.static_folder='static'
self.static_url_path='/static/admin'
# If name is not povided, use capitalized endpoint name # If name is not povided, use capitalized endpoint name
if self.name is None: if self.name is None:
...@@ -383,10 +389,22 @@ class AdminIndexView(BaseView): ...@@ -383,10 +389,22 @@ class AdminIndexView(BaseView):
@expose('/') @expose('/')
def index(self): def index(self):
arg1 = 'Hello' arg1 = 'Hello'
return render_template('adminhome.html', arg1=arg1) return self.render('admin/myhome.html', arg1=arg1)
admin = Admin(index_view=MyHomeView()) admin = Admin(index_view=MyHomeView())
Also, you can change the root url from /admin to / with the following::
admin = Admin(
app,
index_view=AdminIndexView(
name='Home',
template='admin/myhome.html',
url='/'
)
)
Default values for the index page are: Default values for the index page are:
* If a name is not provided, 'Home' will be used. * If a name is not provided, 'Home' will be used.
......
from nose.tools import ok_, eq_, raises from nose.tools import ok_, eq_, raises
from flask import Flask, request, abort from flask import Flask, request, abort, url_for
from flask.views import MethodView from flask.views import MethodView
from flask.ext.admin import base from flask.ext.admin import base
...@@ -133,6 +133,11 @@ def test_admin_customizations(): ...@@ -133,6 +133,11 @@ def test_admin_customizations():
rv = client.get('/foobar/') rv = client.get('/foobar/')
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
# test custom static_url_path
with app.test_request_context('/'):
rv = client.get(url_for('admin.static', filename='bootstrap/bootstrap2/css/bootstrap.css'))
eq_(rv.status_code, 200)
def test_baseview_registration(): def test_baseview_registration():
admin = base.Admin() admin = base.Admin()
...@@ -360,6 +365,11 @@ def test_root_mount(): ...@@ -360,6 +365,11 @@ def test_root_mount():
rv = client.get('/mockview/') rv = client.get('/mockview/')
eq_(rv.data, b'Success!') eq_(rv.data, b'Success!')
# test static files when url='/'
with app.test_request_context('/'):
rv = client.get(url_for('admin.static', filename='bootstrap/bootstrap2/css/bootstrap.css'))
eq_(rv.status_code, 200)
def test_menu_links(): def test_menu_links():
app = Flask(__name__) app = Flask(__name__)
......
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