Login

Front end admin toolbar

Author:
eallik
Posted:
December 1, 2009
Language:
HTML/template
Version:
Not specified
Tags:
admin toolbar
Score:
1 (after 1 ratings)

This snippet will monkeypatch django.db.models.Model to include 7 new methods:

  • get_verbose_name Because you can't access model._meta from templates
  • get_verbose_name_plural
  • get_admin_change_url
  • get_admin_delete_url
  • get_admin_history_url
  • get_admin_changelist_url
  • get_admin_add_url

This snippet also gives you the template code to paste to your base.html so every front end model instance view of your site will show an admin toolbar for logged in users that have admin access.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!--
In your base.html (or whatever base template you are using in your project)
have the following template code, and in your view functions, supply a
model_obj context variable that refers to the primary model object of that
view so base.html will know which model/model object to refer to.
-->

{% if request.user.is_staff %}
<div id="admin-toolbar">
    {% if model_obj %}
    <a href="{{ model_obj.get_admin_change_url }}">Edit</a> |
    <a href="{{ model_obj.get_admin_delete_url }}">Delete</a> |
    <a href="{{ model_obj.get_admin_history_url }}">History</a> |
    <a href="{{ model_obj.get_admin_add_url }}">Add another {{ model_obj.get_verbose_name }}</a> |
    <a href="{{ model_obj.get_admin_changelist_url }}">Manage {{ model_obj.get_verbose_name_plural }}</a>
    {% endif %}
</div>
{% endif %}


<!--
# In your Python code, you need:

from django.db import models
from django.core.urlresolvers import reverse

models.Model._admin_url_name = lambda self, type: 'admin:%s_%s_%s' % (
    self._meta.app_label, self._meta.module_name, type)

def get_admin_change_url(self):
    return reverse(self._admin_url_name('change'), args=(self.pk, ))
models.Model.get_admin_change_url = get_admin_change_url

def get_admin_delete_url(self):
    return reverse(self._admin_url_name('delete'), args=(self.pk, ))
models.Model.get_admin_delete_url = get_admin_delete_url

def get_admin_history_url(self):
    return reverse(self._admin_url_name('history'), args=(self.pk, ))
models.Model.get_admin_history_url = get_admin_history_url

def get_admin_changelist_url(self):
    return reverse(self._admin_url_name('changelist'))
models.Model.get_admin_changelist_url = get_admin_changelist_url

def get_admin_add_url(self):
    return reverse(self._admin_url_name('add'))
models.Model.get_admin_add_url = get_admin_add_url

models.Model.get_verbose_name = lambda self: self._meta.verbose_name
models.Model.get_verbose_name_plural = lambda self: self._meta.verbose_name_plural
-->

More like this

  1. Extended i18n base model by alcinnz 2 years, 3 months ago
  2. Admin action for a generic "CSV Export" by javinievas 4 years, 5 months ago
  3. nginx x-accel-redirect protection of static files by sean 7 years, 8 months ago
  4. Get admin url for a model by timbroder 5 years, 5 months ago
  5. restrict user access to modeladmin via metaclass by code_shogan 4 years, 5 months ago

Comments

eallik (on December 1, 2009):

I don't know how to edit this snippet, so I'll just add a comment:

You also need to from django.core.urlresolvers import reverse in order for the Python part to work.

#

Please login first before commenting.