applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ModelSelect2Widget url

JD99 opened this issue · comments

commented

Привет, не загружаются данные в виджет.

В консоли вижу что запросы уходят. Но идут просто на ту же страницу. Сервер честно ему отвечает шаблоном основной страницы. Возможно где-то нужно указать url по которому нужно забирать данные? Подскажите как решить вопрос.

GET /fields/?field_id=MTQwMTQ4MzY2MjIwMDMy%3A1hXmp1%3ArbO7eVuT0469hnhZuV4AerqvBqs HTTP/1.1" 200 24809

Code Snippet

class FormFilter(forms.Form): 
    things = forms.ModelChoiceField(
        queryset=project_models.Crop.objects.filter(filtervo=True),
        label=u"Crop",
        widget=select2_forms.ModelSelect2Widget(
            search_fields=['crop__icontains'],
            model=project_models.Crop,
        )
    )

commented

в итоге закостылил редиректом

@JD99 Привет! Кор контрибьюторы не говорят по русски, поэтому им будет тяжело понять твой вопрос. Я попробую перевести, но если сможешь уточнить детали, пиши на английском (hey, I am afraid core contributors are not speaking russian, so it would be hard for them to get your question. I'll try to translate, but if you can add more details, please add them in english).

Original question:

I cannot load data to the widget.
I can see that there are requests in the logs, but they are going to the same page. Server returns the full template of a main page. Maybe I should define a certain url somewhere for loading data?

GET /fields/?field_id=MTQwMTQ4MzY2MjIwMDMy%3A1hXmp1%3ArbO7eVuT0469hnhZuV4AerqvBqs HTTP/1.1" 200 24809

Code snippet

class FormFilter(forms.Form): 
    things = forms.ModelChoiceField(
        queryset=project_models.Crop.objects.filter(filtervo=True),
        label=u"Crop",
        widget=select2_forms.ModelSelect2Widget(
            search_fields=['crop__icontains'],
            model=project_models.Crop,
        )
    )

@JD99 please, check the next API reference for HeavySelect2Widget (which is a base for ModelSelect2Widget).

This might get you to the right answer.

thanks @JD99 :)

@JD99 you might want to double check your URL configuration. Make sure the following is added to the beginning of your URL root configuration:

path('select2/', include('django_select2.urls')),
commented

hi

project/urls.py

path(r'select2/', include('django_select2.urls')),
path(r'', include('main.urls')),
path(r'fields/', include('fields.urls'))

fields/urls.py

path(r'', views.view_list, name='fields')

fields/forms.py

class FormFilter(forms.Form):
    cropvo = forms.ModelChoiceField(
        label=u"xxx",
        queryset=project_models.Cropsvo.objects.order_by('cropvo'),
        widget=select2_forms.ModelSelect2Widget(
            model=project_models.Cropsvo,
            search_fields=['cropvo__icontains']
        )
    )

img

commented

temporarily add redirect in fields/urls.py

def view_list(request):
    if 'term' in request.GET or 'field_id' in request.GET:
        path = '/select2{0}auto.json?{1}'.format(
            request.META['PATH_INFO'], request.GET.urlencode())
        return HttpResponseRedirect(path)

Thanks @JD99
can you please also provide the renged HTML output, mostly the data-ajax--url attribute?
Best
-Joe

commented
<select name="user" data-minimum-input-length="2" data-allow-clear="false" data-ajax--url="/select2/fields/auto.json" data-ajax--cache="true" data-ajax--type="GET" data-select2-dependent-fields="district" class="form-control django-select2 django-select2-heavy select2-hidden-accessible" title="" required="" id="id_user" data-field_id="MTQwNDc0Njc2NjUwMDU2:1hck2P:WDMnU0Z58mceTZ-7EX3qseQ0h3I" data-select2-id="id_user" tabindex="-1" aria-hidden="true">
</select>
<select name="district" data-minimum-input-length="2" data-allow-clear="false" data-ajax--url="/select2/fields/auto.json" data-ajax--cache="true" data-ajax--type="GET" class="form-control django-select2 django-select2-heavy select2-hidden-accessible" title="" required="" id="id_district" data-field_id="MTQwNDc0Njg2MzY5NzQ0:1hck2P:ycqsKArmyVt33jJ2Y8Se4eeBChU" data-select2-id="id_district" tabindex="-1" aria-hidden="true">
</select>

That is strange it does point correctly to /select2/fields/auto.json. Did you customize your JavaScript in any way?

commented

Javascript didn't change

commented

Found what the problem was. select2 library was connected in head

<head>
<script src="plugins/select2/select2.min.js"></script>
</head>

and

{{ formfilter.media.js }}

thanks for the help