Admin jQuery Django 1.9.2 select2

1,275 views
Skip to first unread message

Lúcio Henrique

unread,
Feb 21, 2016, 7:07:24 PM2/21/16
to your...@googlegroups.com
Hi,

I’m using last version of dal. I’m trying to use dal-select2 at admin.

I have create a new default project, with default admin template and configs. I didn’t change anything.

After configuration to use dal, I have problem with jQuery/select2. Check my Console log:
Select2: An instance of jQuery or a jQuery-compatible library was not found. Make sure that you are including jQuery before Select2 on your web page.(anonymous function) @ select2.full.js:473
select2.full.js:6022 Uncaught TypeError: Cannot read property 'event' of undefined
select2.js:8 Uncaught TypeError: $(...).select2 is not a function

This is the source-code from generated admin template:
<!DOCTYPE html>

<html lang="pt-br" >
<head>
<title>Adicionar Estado | Site de administração do Django</title>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/forms.css" />


<script type="text/javascript" src="/admin/jsi18n/"></script>
<link href="/static/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link href="/static/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.min.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>

Autocomplete and jQuery are loaded correctly. The output html is the same located at example: dal-yourlabs.rhcloud.com/admin/linked_data/testmodel/2/change/

Everything seems to be OK. What is the problem with jQuery and Select2 and Admin?

My configs and files:

INSTALLED_APPS = [
    'dal',
    'dal_select2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cadastro',
]

cadastro/urls.py
from dal import autocomplete

from django.conf.urls import url

from models import Pais


class LinkedDataView(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = super(LinkedDataView, self).get_queryset()

        return qs


urlpatterns = [
    url(
        '^linked_data/$',
        LinkedDataView.as_view(model=Pais),
        name='linked_data'
    ),
]

cadastro/forms.py
#coding:utf-8
from django import forms
from models import Estado
from dal import autocomplete

class EstadoForm(forms.ModelForm):


    class Meta:
        model = Estado
        fields = '__all__'
        widgets = {
            'Pais': autocomplete.ModelSelect2(url='linked_data')
        }

cadastro/admin.py
from django.contrib import admin

from models import Pais, Estado
from forms import EstadoForm
# Register your models here.

class EstadoAdmin(admin.ModelAdmin):
    form = EstadoForm

admin.site.register(Estado, EstadoAdmin)

admin.site.register(Pais)


urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^teste/', include('cadastro.urls')),
]

James Pic

unread,
Feb 22, 2016, 8:24:16 AM2/22/16
to yourlabs
What version of DAL ?

What's the response for the admin/js/jquery.init.js static file ?

Lúcio Henrique

unread,
Feb 22, 2016, 8:41:47 AM2/22/16
to your...@googlegroups.com
I´m using 3.0.4 (via pip).

Response:

"GET /static/admin/js/jquery.init.js HTTP/1.1" 304 0


The content of jQuery.init.js is:

/*global django:true, jQuery:false*/
/* Puts the included jQuery into our own namespace using noConflict and passing
 * it 'true'. This ensures that the included jQuery doesn't pollute the global
 * namespace (i.e. this preserves pre-existing values for both window.$ and
 * window.jQuery).
 */
var django = django || {};
django.jQuery = jQuery.noConflict(true);

Em 22 de fevereiro de 2016 no 10:24:16, James Pic (jame...@gmail.com) escreveu:

What version of DAL ?

What's the response for the admin/js/jquery.init.js static file ?
--
You received this message because you are subscribed to the Google Groups "yourlabs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yourlabs+u...@googlegroups.com.
To post to this group, send email to your...@googlegroups.com.
Visit this group at https://groups.google.com/group/yourlabs.
To view this discussion on the web visit https://groups.google.com/d/msgid/yourlabs/CALC3Kac99AYjUYRJ1ONmYg7hsHw9Y6V7%2B2qJn5F6qxyE%3DNQu8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

James Pic

unread,
Feb 22, 2016, 8:54:42 AM2/22/16
to yourlabs

Lúcio Henrique

unread,
Feb 22, 2016, 9:38:13 AM2/22/16
to your...@googlegroups.com

In my project static files folder, I created static/admin/js/jquery.init.js whit content of https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal/static/admin/js/jquery.init.js 

So, I run collectstatic and now it works.

Em 22 de fevereiro de 2016 no 10:54:42, James Pic (jame...@gmail.com) escreveu:

--
You received this message because you are subscribed to the Google Groups "yourlabs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yourlabs+u...@googlegroups.com.
To post to this group, send email to your...@googlegroups.com.
Visit this group at https://groups.google.com/group/yourlabs.

James Pic

unread,
Feb 22, 2016, 6:11:29 PM2/22/16
to yourlabs
On Mon, Feb 22, 2016 at 3:38 PM, Lúcio Henrique <luc...@gmail.com> wrote:
>
> The fact is that
> https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal/static/admin/js/jquery.init.js
> in my case do not replace admin/js file…

I'd love to see this reproduced in a project, as long as dal is before
contrib.admin it should work from my experience.

--
http://yourlabs.org
Customer is king - Le client est roi - El cliente es rey.

Erick Vásquez

unread,
Mar 22, 2016, 5:04:46 PM3/22/16
to yourlabs
Hi, Could you solve this problem?

James Pic

unread,
Mar 22, 2016, 5:09:42 PM3/22/16
to yourlabs
Couldn't reproduce it​.

Will Hieronymus

unread,
Feb 8, 2017, 11:56:28 AM2/8/17
to yourlabs
Lucio, delete the jquery.init.js file from your storage location then run collect static again. As is_null pointed out as long as dal is before contrib.admin in installed apps this time when the collectstatic is run. It should copy only the dal jquery.init.js file and ignore the contrib.admin file.

See this isssue on their github for more info:
https://github.com/yourlabs/django-autocomplete-light/issues/602

On Tuesday, March 22, 2016 at 5:09:42 PM UTC-4, is_null wrote:
Couldn't reproduce it​.

Reply all
Reply to author
Forward
0 new messages