本文介绍了Django-autocomplete-light显示为空下拉列表,而不是autocomplete小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中实现django-autocomplete-light,但无法弄清楚为什么它不向我显示自动完成小部件,而是一直显示空的下拉列表.

I am trying to implement django-autocomplete-light in my projects but cannot figure out why it does not show me the autocomplete widget, but keeps showing an empty dropdown.

我遵循了该教程: https://django-autocomplete-light.readthedocs.io/en/3.1.3/tutorial.html .

我发现此问题已在其他stackoverflow问题中发生,但到目前为止,这些答案都没有帮助我.

I found that this problem has occurred in other stackoverflow questions, but none of those answers have helped me so far.

我有以下模型:

class Vilt(models.Model):
    vilt_title = models.CharField(max_length=200, unique=True)

我创建了此自动完成视图:

I created this autocomplete view:

class ViltAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        # if not self.request.user.is_authenticated():
        #     return Vilt.objects.none()
        qs = Vilt.objects.all().order_by('vilt_title')

        if self.q:
            qs = qs.filter(vilt_title__istartswith=self.q)

        return qs

我在指定小部件的地方使用这个ModelForm.

I use this ModelForm where I specify the widget.

from .models import Vilt
from dal import autocomplete

class ViltSearchForm(forms.ModelForm):
  vilt_title = forms.ModelChoiceField(
    queryset = Vilt.objects.all(),
    widget = autocomplete.ModelSelect2(url='vilt-autocomplete')
  )

  class Meta:
    model = Vilt
    fields = ('vilt_title',)

从.views导入

from .views import (ViltAutocomplete,
                    )
urlpatterns = [
    #other paths
    path('vilt/autocomplete/', ViltAutocomplete.as_view(), name='vilt-autocomplete'),
    #other paths
]
{% extends "bierviltje/base.html" %}
{% load static %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container">
#other forms
    <div>
        <form action="" method="post">
            {% csrf_token %}
            {{ vilt_search_form|crispy }}
            <input type="submit" />
        </form>
    </div>
#other forms
</div>
{% endblock content %}

{% block javascript %}
{{ vilt_search_form.media }}
{% endblock javascript %}

这是在base.html中的javascript块之前加载的Javascript:

This is the Javascript that is loaded in before the javascript block in base.html:

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
{% block javascript %}
{% endblock javascript %}

推荐答案

自您发布查询以来已经有一段时间了.但是如果您还没有找到答案,请使用以下解决方案:

It's been sometime since you posted your query. But in case you have not found the answer yet, here is the solution:

在您的ModelForm"ViltSearchForm"中,请从更改:

In your ModelForm "ViltSearchForm", please change the widget from:

widget = autocomplete.ModelSelect2(url='vilt-autocomplete')

:

widget = autocomplete.ListSelect2(url='vilt-autocomplete')

但是,如果可以在这里添加,我无法理解在独立模型上使用自动完成"的原因.

However, if I may add here, I can't fathom the reason for using "autocomplete" on a stand alone model.

非常感谢您提供反馈.

这篇关于Django-autocomplete-light显示为空下拉列表,而不是autocomplete小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:32