Login

All snippets written in JavaScript

Snippet List

Disable fields in oldforms admin using jQuery

This snippet shows how to disable fields in a edit page in the oldforms admin using jquery. The idea is to add the javascript to the edit page using the `js` attribute of the model's `Admin` class. In this case jQuery and a custom javascript file are added. The javascript sets the `disabled` attribute of the `name` field to `true` as soon as the document is ready.

  • admin
  • jquery
  • oldforms
  • disable
Read More

simple jquery example

Displays an input with an add button. Clicking the add button creates a simple form - input with a submit button. Clicking the submit button will send the output to the server and return a value which is displayed.

  • ajax
  • jquery
Read More

Who's helping a lot in IRC

We had some fun today on the #django IRC channel searching and counting through past logs for people saying "thanks" to [a known very helpful person](http://djangopeople.net/magus/). Here's a unix shell script for checking your own score if you're using Pidgin and have logging turned on. Replace ".purple" with ".gaim" in the script if you're using Gaim (an older version of Pidgin).

  • log
  • help
  • irc
  • thanks
  • score
Read More

In page edit object links

This javascript has the functionality of the 'Edit Object' bookmarklet as [described by James Bennett](http://www.b-list.org/weblog/2007/nov/07/bookmarklets/) but instead of putting it within a bookmarklet it creates a div on the page with a link to the main admin screen, an edit the object link, and logout link. Just include the javascript on any page you want the links to show up on and make sure that the page is using the populate_xheaders function to provide the needed headers as [described here](http://www.b-list.org/weblog/2007/nov/07/bookmarklets/). The built in generic views provide the needed headers by default.

  • admin
  • edit-object-links
Read More

Ajax progress bar

This snippet is an example of an ajax progress bar (using jquery) that you might use in conjunction with <http://www.djangosnippets.org/snippets/678/>. 1. Generates a uuid and adds X-Progress-ID to the forms action url. 2. Adds the progress bar to the page. (you'll have to add some css styling for this) 3. Makes period ajax requests to update the progress bar.

  • ajax
  • javascript
  • upload
  • file
  • progress
Read More

Add special field lookups to the Admin list_filter display

Ever wanted to add an atypical [field lookup](http://www.djangoproject.com/documentation/db-api/#field-lookups) to the Django Admin list_filter filters, like `__isnull` or `__in`? This jQuery snippet allows you to do just that. Since you can access those additional filters by directly typing them into in the Admin URL, the tricky part is to add those to the regular list_filter display. A lot of this code is spent checking on querystring matches which is ugly and error-prone -- if you see any problems or room for improvement, drop me a comment! A suggestion of where to place this code is in `templates/admin/yourapp/yourmodel/change_list.html`. Mine kinda looks like this: {% extends "admin/change_list.html" %} {% block content %} <script src="/static/js/jquery-1.2.2.min.js" type="text/javascript"></script> <script type="text/javascript"> // the JavaScript posted in this snippet </script> {{ block.super }} {% endblock %}

  • admin
  • jquery
Read More

Admin list_display Ajax

Sometimes it can be time consuming to go through a bunch of objects in Django's Admin if you only need to update one field in each. An example of this is an `order` field that allows you to manually set the order for a queryset. **This snippet contains examples of how to set up the Admin list_display to make Ajax calls to a custom view.** The following code may not be worthy of being a snippet, it's all pretty straightforward, but here it is for those who just want to copy-and-paste.

  • admin
  • jquery
Read More

Unobtrusvie Foldable Admin Interface

Inspired by [snippet 550](http://www.djangosnippets.org/snippets/550/), this allows you to expand or collapse apps in the main Admin screen. Requires jQuery. If jquery.cookie.js is available it will remember which apps you have expanded. Recommended usage: Place the JavaScript in a file called `admin-expand.js`. Create `templates/admin/base_site.html` in your templates directory (which is also a good place to [brand your Admin](http://djangobook.com/en/1.0/chapter06/#cn70)). Put the following code near the top, and you're done (adjusting file paths as needed). {% extends "admin/base.html" %} {% block extrahead %} <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript" src="jquery.cookie.js"></script> <script type="text/javascript" src="admin-expand.js"></script> {% endblock %}

  • admin
  • jquery
Read More

Get Latitude and Longitude from google maps

I use this to integrate a google map in a form renderd by newforms. If someone click on the map and set a icon, the Latitude and Longitude data goes into the 2 form fields. You need to generate a google API key (http://www.google.com/apis/maps/signup.html) for the GOOGLE_KEY variable.

  • google
  • latitude
  • longitude
  • googlemaps
Read More
Author: b23
  • 1
  • 14

admin: edit related object shortcut

Adds a shortcut to edit releated objects right/ForeignKey fields. an edit symbol will be shown right next to the "add annother" link on all select boxes, with opens the releated object currently selected in a popup window. **depends on jquery** Add this to the head of "templates/admin/base.html". you may need to add `<script type="text/javascript" src="/path/to/jquery.js"></script>` before it

  • admin
  • jquery
  • releated-objects
Read More

jQuery Autocomplete

A merged version of the many jQuery autocomplete widgets. See [1](http://php.scripts.psu.edu/rja171/widgets/autocomplete.php) and [2](http://www.dyve.net/jquery/?autocomplete) for more information.

  • jquery
  • autocomplete
Read More

More admin clock time increments

This is quite a hack (need to modify Django code), but relatively simple and stable. It displays various times in whichever increments and columns you specify, rather than just Midnight, Noon, Now & 6am. You can use it throughout your admin and newforms-admin code. This is a (slightly updated) copy of the ticket #1848 which wasn't included in trunk. http://code.djangoproject.com/ticket/1848

  • admin
  • time
Read More

Client-side Django-style date & time string formatting

This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting. The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T". As a 'dumb' illustration, the following template tag usage: It is {% now "jS F Y H:i" %} ...could equate to the following: It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script> It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.

  • javascript
  • dynamic
  • datetime
  • date
  • format
  • time
  • string
  • now
Read More

jQuery ajax search

This is the result of my first tests with jQuery and Django. After entering a search term it gets search results using ajax and json. Then is uses the rather crude `result_table` function to generate a table of results. Django is on the serverside for generating json

  • ajax
  • jquery
Read More

60 snippets posted so far.