Package: django-staff-toolbar
django-staff-toolbarDisplaying staff-only controls at a webpage.Features:Linking to the admin page of the current object.Full configuration of the displayed toolbar items.API for adding custom menu items.InstallationFirst install the module, preferably in a virtual environment:pipinstalldjango-staff-toolbarConfigurationAdd the application tosettings.py:INSTALLED_APPS+=('staff_toolbar',)Make sure thedjango.core.context_processors.requestis included inTEMPLATE_CONTEXT_PROCESSORS.Add the HTML widget to the template:{%loadstaff_toolbar_tags%}{%staff_toolbar%}Make sure the layout is loaded in the template:<linkrel="stylesheet"type="text/css"href="{{STATIC_URL}}staff_toolbar/staff_toolbar.css"/>LayoutBy default, a simple layout is included.
You can change this layout to your own liking.The source SASS file is included, making it easier to
integrate this into your project stylesheets when needed.Customizing the admin URLThe admin URL is auto-detected using:Theobjectvariable in the template.Theview.objectvariable in the template.In some cases, this is not sufficient. When the auto-detected “Change object”
link does not point to the right page, this can be resolved using two methods:Using the viewWhen your class-based-view implementsstaff_toolbar.views.StaffUrlMixin,
that information will be used to render the proper “Change object” link.This requires Django 1.5, which exports theviewvariable to the template.Using the templateIn the template, you can include:{%set_staff_objectpage%}When needed, the URL can also be set:{%set_staff_url%}{%url'dashboard:catalogue-product'object.id%}{%end_set_staff_url%}Customizing the menuThe default menu settings are:STAFF_TOOLBAR_ITEMS=('staff_toolbar.items.AdminIndexLink','staff_toolbar.items.ChangeObjectLink','staff_toolbar.items.LogoutLink',)Each line represents a callable, which is called using(request, context).
When a tuple is included, this is converted into a newGroupobject,
causing an additional<ul>tag to appear in the output.A more complex example:fromdjango.core.urlresolversimportreverse_lazyfromdjango.utils.translationimportugettext_lazyas_fromstaff_toolbarimporttoolbar_item,toolbar_link,toolbar_title,toolbar_literalSTAFF_TOOLBAR_ITEMS=('staff_toolbar.items.AdminIndexLink','staff_toolbar.items.ChangeObjectLink',(toolbar_title(_("User")),toolbar_link(url=reverse_lazy('admin:password_change'),title=_("Change password")),'staff_toolbar.items.LogoutLink',))Thetoolbar_title()andtoolbar_item()functions allow to pass additional arguments
to the items, without having to load them already in the settings.It’s also perfectly possible to instantiate the actual classes directly,
however this may risk import errors as it causes your settings module to load a lot of other code.
The following is functionally equivalent to the previous example:fromdjango.core.urlresolversimportreverse_lazyfromdjango.utils.translationimportugettext_lazyas_fromstaff_toolbar.itemsimportAdminIndexLink,ChangeObjectLink,Group,ToolbarTitle,Link,LogoutLinkSTAFF_TOOLBAR_ITEMS=(AdminIndexLink(),ChangeObjectLink(),Group(ToolbarTitle(_("User")),Link(url=reverse_lazy('admin:password_change'),title=_("Change password")),LogoutLink(),))CaveatsFor HTTPS sites withSESSION_COOKIE_SECURE = Truethe toolbar obviously
won’t show up in the standard pages that are served by HTTP.Either display all pages on HTTPS (which is the Right Way™ after all),
or please provide a good pull request that solves this nicely for mixed sites.ContributingThis module is designed to be generic, and easy to plug into your site.
Pull requests and improvements are welcome!If you have any other valuable contribution, suggestion or idea, please let us know as well!