django 自动完成灯无法输入字符串

问题描述

我已经尝试按照文档指南使用 django-autocomplete-light,但最后我无法输入字符串来搜索数据,这里是图片

enter image description here

我无法输入字符串来搜索数据。这是我的代码

views.py

    from django.shortcuts import render
    from .models import *
    from .forms import PendaftaranForm
    from dal import autocomplete

    # Create your views here.
    class DesaAutocomplete(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 Country.objects.none()

            qs = Desa.objects.all()

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

            return qs

models.py

class Desa(models.Model):
    nama = models.CharField(max_length=30)
    kecamatan = models.ForeignKey(Kecamatan,null=True,blank=True,on_delete=models.CASCADE)

    def __str__(self):
        return self.nama

urls.py

from django.urls import path
from django.conf.urls import url
from . import views

urlpatterns = [
    path('',views.index,name='index'),url(
        r'^desa-autocomplete/$',views.DesaAutocomplete.as_view(),name='desa-autocomplete',),]

forms.py

from django import forms
from .models import *
from tempus_dominus.widgets import DatePicker
from dal import autocomplete

class PendaftaranForm(forms.ModelForm):
    class Meta:
        model=Pendaftaran
        fields= (
            'tanggal_pendaftaran','nama','desa','kecamatan','mutasi','jumlah','keterangan','tanggal_selesai',)
        widgets =  {
            'tanggal_pendaftaran':DatePicker(
                attrs={
                'append': 'fa fa-calendar','icon_toggle': True,}
            ),'nama':forms.TextInput(
                attrs={
                    'class':'form-control','desa':autocomplete.ModelSelect2(
                url='desa-autocomplete',attrs={
                    'class':'form-control',...

如何输入要搜索的字符串?我的代码有什么问题吗?感谢您的帮助。

解决方法

确保元素上没有使用冲突的 CSS,例如 bootstrap modal。