Tagging Autocomplete to work with Django 1.2

27th October 2010 in Coding Tags: django, jquery, programming, python

If you are using Django 1.2 and you tried installing Django Tagging Autocomplete you will have noticed that the autocompletition won't work unless you load a second copy of jQuery.

Loading a second copy of jQuery is quite an inelegant (to use an euphemism) solution and doesn't suit the paradigm of Django. 

Why do I get "$ is undefined", "jQuery is undefined"?

Because Django 1.2 uses a different jQuery namespace to avoid conflicts with other third party apps.

How to solve that?

The fix is easy, just replace the file widgets.py of the tagging_autocomplete app with this code (changes described with two comments):

from django.forms.widgets import Input
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.safestring import mark_safe

class TagAutocomplete(Input):
	input_type = 'text'
	
	def render(self, name, value, attrs=None):
		json_view = reverse('tagging_autocomplete-list')
		html = super(TagAutocomplete, self).render(name, value, attrs)
        # in the following line I added (django.jQuery); in the end
		js = u'<script type="text/javascript">(function($) { $("#%s").autocomplete("%s", { multiple: true }); })(django.jQuery);</script>' % (attrs['id'], json_view)
		return mark_safe("\n".join([html, js]))
	
	class Media:
		js_base_url = getattr(settings, 'TAGGING_AUTOCOMPLETE_JS_BASE_URL','%s/jquery-autocomplete' % settings.MEDIA_URL)
		css = {
		    'all': ('%s/jquery.autocomplete.css' % js_base_url,)
		}
		js = (
			# this line can be removed as Django 1.2 natively includes jQuery - '%s/lib/jquery.js' % js_base_url,
			'%s/jquery.autocomplete.js' % js_base_url,
		)

The next step is to replace (jQuery); with (django.jQuery); at the very end of jquery.autocomplete.js.

Why tagging autocomplete hasn't been updated yet

This change breaks compatibility with Django 1.1 and that's why the author still didn't want to upgrade the app (27 October 2010).

Retweet

Comments

Leave your comment

Categories

Let's be social

Popular posts

Latest Comments

  1. nice site i really learn a lot more power to your blog <a href="http://staying-healthy.info">healthy living</a>

    By health is wealth in Nice Skip Links Appearing on :focus with CSS

  2. Very strange bug- thanks for the fix!

    By Andrew Chart in IE8 doesn't like 1x1px semi-transparent backgrounds

  3. Completely agree about keeping it simple, sometimes people try a little too hard

    By ryan in 10 Effective Business Card Design Tips

  4. I just wanted to say that your blog has been really useful.

    By Kerala in Django: How to Retrieve Query String Parameters

  5. Thank you this was exactly what I have been searching for.

    By Kerala in Django Tagging Autocomplete Tag-It

Popular Tags

creativity django google-summer-of-code ibiza inspiration jquery nemesisdesign ninux open-source performance photo programming python security staticgenerator upload wireless-community