Login

Adding buttons to submit line in a Admin page

Author:
marinho
Posted:
May 12, 2008
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

Attention: this snippet depends on jQuery library to works

The Admin templates hierarchy does not allow you change the submit line (that buttons bar in the footer of the change view of a model class).

This code shows how to resolve this, using a simple javascript trick. You can read the code and implement using your favorite javascript library (instead of jQuery) or pure JavaScript.

This example regards to User Profile, a common used model class that adds custom fields to a user in the authentication system.

To use this, you must put this template code in a file with the following name:

templates/admin/auth/user/change_form.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{% extends "admin/change_form.html" %}
{% load i18n %}

{% block extrahead %}{{ block.super }}

<script src="/media/js/jquery.js"></script>
<script>
    $(document).ready(function() {
        $('.move_to_submit_row').insertBefore('.submit-row>input:first');
    });
</script>
{% endblock extrahead %}

{% block after_field_sets %}{{ block.super }}

<input value="{% trans "Profile" %}"
 onclick="javascript: window.location='/admin/central/userprofile/{{ original.get_profile.id }}/'"
 class="move_to_submit_row" type="button"/>
{% endblock after_field_sets %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

itavor (on May 14, 2008):

Nice! I already include jQuery in admin for most of my sites and use it to do other customizations, this is very easy to add and very useful.

#

Please login first before commenting.