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

Comments are closed.

Comments have been closed for this post.

Categories

Let's be social

Popular posts

Latest Comments

  1. I got very good results with this, thanks for sharing.

    By Yasir Atabani in How to speed up tests with Django and PostgreSQL

  2. Hi Amad, for any question regarding OpenWISP, use one of the support channels: http://openwisp.org/support.html

    By Federico Capoano in How to install OpenWISP

  3. Sir please guid , i have install the ansible-openwisp2 , now how to add the access points . What is the next procedure . Please help.

    By Ahmad in How to install OpenWISP

  4. Hi Ronak, for any question regarding OpenWISP, use one of the support channels: http://openwisp.org/support.html

    By Federico Capoano in netjsonconfig: convert NetJSON to OpenWRT UCI

  5. Hi, I have installed openwisp controller using ansible playbook. Now, i am adding the configurations automatically using OPENWRT devices in openwisp file by specifying shared_key so can you suggest me if I want to set limit to add configuration how can i do it?

    By Ronak in netjsonconfig: convert NetJSON to OpenWRT UCI

Popular Tags

battlemesh censorship creativity criptography django event fosdem google-summer-of-code ibiza inspiration javascript jquery linux nemesisdesign netjson ninux nodeshot open-source openwisp openwrt performance photo programming python security staticgenerator talk upload wifi wireless-community