Django Admin Copy Button

Using javascript I created a copy button to extend the change_form.html pages. Here is the code. Comment if you need more details.

1 {% extends "admin/change_form.html" %} 2 {% block extrahead %} 3 <script type="text/javascript"> 4 window.onload = function() { 5 6 // already an add page 7 var docLoc = document.location.href; 8 if( docLoc.indexOf('/add/')+5 == docLoc.length ) 9 return; 10 11 // add button 12 var domOrig = document.getElementsByName( '_continue' ); 13 var domAdd = document.getElementsByName( '_addanother' ); 14 15 // should only be one 16 if( !domOrig || !domOrig[0] ) return; 17 domOrig = domOrig[0]; 18 19 if( !domAdd || !domAdd[0] ) return; 20 domAdd = domAdd[0]; 21 22 // copy the continue button 23 var domClone = document.createElement( 'input' ); 24 domClone.type = domOrig.type; 25 domClone.size = domOrig.size; 26 domClone.name = domOrig.name; 27 domClone.className = domOrig.className; 28 domClone.value = 'Copy and continue editing'; 29 30 // insert before add button 31 domOrig.parentNode.insertBefore(domClone,domAdd); 32 33 // on click event 34 domClone.onclick = function() { 35 // location, form, action 36 var docLoc = document.location.href; 37 var domForm = document.forms[0] 38 var strAction = docLoc.substring( 0, docLoc.lastIndexOf('/',docLoc.length-2) ) + '/add/'; 39 40 // copy button ~ add button and will fail on unique-ness-es 41 // unless uniques are modified on page 42 domForm.action = strAction; 43 domForm.submit(); 44 return( true ); 45 } 46 } 47 </script> 48 {% endblock %}

Post a Comment


     __  __    __    _  _  ____  _____  _  _    _____  ____   ___ 
    (  \/  )  /__\  ( \( )(_  _)(  _  )( \( )  (  _  )(  _ \ / __) 
     )    (  /(__)\  )  (  _)(_  )(_)(  )  (    )(_)(  )   /( (_-.
    (_/\/\_)(__)(__)(_)\_)(____)(_____)(_)\_)()(_____)(_)\_) \___/