Skip to main content
 首页 » 编程设计

python之Django-Autocomplete-light 小部件未显示

2024年08月22日30qq号

我刚刚安装了 Django-Autocomplete-Light 并按照其 site 上的指南进行操作.我已经设置了自动完成 View 并且它按预期工作(返回自动完成选项的 JASON)。不过,我似乎无法让小部件显示出来。进出管理面板。

一切都没有错误地编译,但我得到的是一个空的下拉选择框,而不是小部件。

Image of empty selection box in admin panel

这是我的设置.py:

INSTALLED_APPS = [ 
    'dal', 
    'dal_select2', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'selectable', 
    'videos.apps.VideosConfig', 
] 

这是 views.py:

class ActorAutocomplete(autocomplete.Select2QuerySetView): 
    def get_queryset(self): 
        # Don't forget to filter out results depending on the visitor ! 
 
        qs = Actor.objects.all() 
 
        if self.q: 
            qs = qs.filter(name__istartswith=self.q) 
 
        return qs 

urls.py:

url(r'^actor-autocomplete/$', views.ActorAutocomplete.as_view(), name='actor-autocomplete'), 

表单.py:

class ActorForm(forms.ModelForm): 
    class Meta: 
        model = Actor 
        fields = ('name',) 
        widgets = { 
            'name': autocomplete.Select(url='videos/actor-autocomplete') 
        } 

感觉好像漏掉了一些简单的东西,但又不知道是什么。也许它无法访问静态文件,但根据文档,将“dal”添加到 settings.py 中的 installed_app 应该可以解决这个问题。 我将不胜感激!

我忘了说 Actor.name 是 CharField 类型。也许我没有使用正确的小部件,但我还没有找到特定于 CharField 的小部件。

As I have the same problem, here is the content of my console in Firefox:

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.

It seems that jQuery is needed but not found, but I can't find a way to link it properly.

It is also running perfectly in Chromium. I don't have any other browser to test.

请您参考如下方法:

经过一些测试,我发现这在 Firefox 和 Chrome 隐身模式下运行良好。我已经禁用了所有 chrome 扩展,现在它也适用于我的常规 chrome。

问题很可能是其中一个扩展,但我没有设法将其缩小到特定的扩展。

长话短说 更改浏览器或从其他 Chrome 用户打开网站可以解决我的问题。