Login

"Save and Continue" keyboard command for admin, with autoscroll

Author:
jcushman
Posted:
January 7, 2010
Language:
JavaScript
Version:
Not specified
Tags:
admin jquery save
Score:
2 (after 2 ratings)

This snippet is helpful if you do a lot of editing on a single large admin form (for example, in a rich text field), and want to frequently save your progress. If you press control-S, or command-S on a Mac, the admin change form will save and reload, and the page will scroll back down to where you last were.

This snippet relies on jquery, jquery.cookie, and the shortcut.js keyboard library (which doesn't use jquery, but seemed more robust than the jquery keyboard plugins I saw). It uses a temporary cookie to remember where the page was scrolled to, to avoid having to override the admin behavior.

Note: don't put this in templates/admin/change_form.html -- the circular import causes a Django crash.

Edit: Had forgotten to include jquery.cookie, which I was already including elsewhere.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
In a change_form template, such as templates/admin/myapp/change_form.html:

{% extends "admin/change_form.html" %}

{% block extrahead %}
	{{ block.super }}
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
    <script src="{{MEDIA_URL}}js/jquery.cookie.js" type="text/javascript"></script>
    <script src="{{MEDIA_URL}}js/shortcut.js" type="text/javascript"></script>
    <script type="text/javascript">
		$(function(){
			setTimeout(function(){
					$(window).scrollTop($.cookie('django_admin_scroll'));
					$.cookie('django_admin_scroll', 0);
				}, 100);
		});
		function save_and_continue(){
			$.cookie('django_admin_scroll',$(window).scrollTop());
			$('input[name="_continue"]').click()
		}
		shortcut.add("Meta+S", save_and_continue);
		shortcut.add("Ctrl+S", save_and_continue);
    </script>
{% endblock %}

More like this

  1. FieldsetForm by Ciantic 7 years, 11 months ago
  2. Django using admin horizontal filter in forms by crodjer 3 years, 8 months ago
  3. Admin Input Field Character Count via jQuery by joshman 6 years, 2 months ago
  4. Make hyperlinks for labels of raw_id_fields (jQuery) by ramen 5 years, 2 months ago
  5. Confirm alert if the user navigates away without saving changes by mrazzari 5 years, 7 months ago

Comments

Please login first before commenting.