Python 如何在具有多个字符域的窗体上使用django autocomplete light

Python 如何在具有多个字符域的窗体上使用django autocomplete light,python,django,django-forms,django-autocomplete-light,Python,Django,Django Forms,Django Autocomplete Light,我已经安装了django自动完成灯 没有自动完成的表单如下所示: class FilterForm(forms.Form): form_origin = forms.CharField(label='From country', max_length=100, required=False,

我已经安装了django自动完成灯

没有自动完成的表单如下所示:

class FilterForm(forms.Form):
    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'From country',
                                      'arial-label':'C2',
                                  }))
    form_target = forms.CharField(label='To country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'To country',
                                      'arial-label': 'C1',
                                  }))
class Country(models.Model):
    iso3 = models.CharField('3 Digit ISO', max_length=3, unique=True)
    name = models.CharField('Name', max_length=50)
    lon = models.FloatField()
    lat = models.FloatField()

    def __str__(self):
        return self.name
{% for field in form %}
     <div class="form-inline form-group">
            <div class="col-sm-10">
                  {{ field|attr:"class:form-control" }}
             </div>
    </div>
{% endfor %}
form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ModelSelect2(url='YOUR_APP_NAME:CountryAutocomplete'))
我在后面写了一个视图,它给出了所有以同一个字母开头的国家,作为我的输入

class CountryAutocomplete(autocomplete.Select2QuerySetView): 
    def get_queryset(self):
        qs = Country.objects.all()
        qs = qs.filter(name__istartswith=self.q)
        return qs
但是,我在表单中使用它时遇到了问题,表单有两个字符字段,两个字段都使用名为
Country
的相同模型。本教程并不是针对这一点量身定制的。本质上,我希望有两个文本框,自动完成国家的往返,有点像skyscanner。任何正确方向的建议都将不胜感激

我的型号如下所示:

class FilterForm(forms.Form):
    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'From country',
                                      'arial-label':'C2',
                                  }))
    form_target = forms.CharField(label='To country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'To country',
                                      'arial-label': 'C1',
                                  }))
class Country(models.Model):
    iso3 = models.CharField('3 Digit ISO', max_length=3, unique=True)
    name = models.CharField('Name', max_length=50)
    lon = models.FloatField()
    lat = models.FloatField()

    def __str__(self):
        return self.name
{% for field in form %}
     <div class="form-inline form-group">
            <div class="col-sm-10">
                  {{ field|attr:"class:form-control" }}
             </div>
    </div>
{% endfor %}
form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ModelSelect2(url='YOUR_APP_NAME:CountryAutocomplete'))
更新:

我的网址是:

re_路径(r'^country-autocomplete/$,views.CountryAutocomplete.as_-view(),name='country-autocomplete'),

在Sumit的帮助下,我已将表单更新为:

    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ListSelect2(
                                      url='myapp:country-autocomplete',
                                  ))
但是,我得到一个下拉列表,其中没有可选择的字段:

我正在使用引导,我的模板如下所示:

class FilterForm(forms.Form):
    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'From country',
                                      'arial-label':'C2',
                                  }))
    form_target = forms.CharField(label='To country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'To country',
                                      'arial-label': 'C1',
                                  }))
class Country(models.Model):
    iso3 = models.CharField('3 Digit ISO', max_length=3, unique=True)
    name = models.CharField('Name', max_length=50)
    lon = models.FloatField()
    lat = models.FloatField()

    def __str__(self):
        return self.name
{% for field in form %}
     <div class="form-inline form-group">
            <div class="col-sm-10">
                  {{ field|attr:"class:form-control" }}
             </div>
    </div>
{% endfor %}
form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ModelSelect2(url='YOUR_APP_NAME:CountryAutocomplete'))
{%用于表单%]中的字段
{field | attr:“类:表单控件”}
{%endfor%}

您的自动完成表单是什么样子的?或者你还没有把它放在里面?如果您不喜欢,您可以这样尝试:

class FilterForm(forms.Form):
    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'From country',
                                      'arial-label':'C2',
                                  }))
    form_target = forms.CharField(label='To country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'To country',
                                      'arial-label': 'C1',
                                  }))
class Country(models.Model):
    iso3 = models.CharField('3 Digit ISO', max_length=3, unique=True)
    name = models.CharField('Name', max_length=50)
    lon = models.FloatField()
    lat = models.FloatField()

    def __str__(self):
        return self.name
{% for field in form %}
     <div class="form-inline form-group">
            <div class="col-sm-10">
                  {{ field|attr:"class:form-control" }}
             </div>
    </div>
{% endfor %}
form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ModelSelect2(url='YOUR_APP_NAME:CountryAutocomplete'))
对目的地也做同样的事情

另外,在URL.py name中,请确保声明如下:

path('CountryAutocomplete/', views.CountryAutocomplete.as_view(), name = 'CountryAutocomplete'),

您的自动完成表单是什么样子的?或者你还没有把它放在里面?如果您不喜欢,您可以这样尝试:

class FilterForm(forms.Form):
    form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'From country',
                                      'arial-label':'C2',
                                  }))
    form_target = forms.CharField(label='To country',
                                  max_length=100,
                                  required=False,
                                  widget=forms.TextInput(attrs={
                                      'placeholder': 'To country',
                                      'arial-label': 'C1',
                                  }))
class Country(models.Model):
    iso3 = models.CharField('3 Digit ISO', max_length=3, unique=True)
    name = models.CharField('Name', max_length=50)
    lon = models.FloatField()
    lat = models.FloatField()

    def __str__(self):
        return self.name
{% for field in form %}
     <div class="form-inline form-group">
            <div class="col-sm-10">
                  {{ field|attr:"class:form-control" }}
             </div>
    </div>
{% endfor %}
form_origin = forms.CharField(label='From country',
                                  max_length=100,
                                  required=False,
                                  widget=autocomplete.ModelSelect2(url='YOUR_APP_NAME:CountryAutocomplete'))
对目的地也做同样的事情

另外,在URL.py name中,请确保声明如下:

path('CountryAutocomplete/', views.CountryAutocomplete.as_view(), name = 'CountryAutocomplete'),

非常感谢你的帮助。我尝试过使用这个解决方案,还添加了url。但是我遇到了一个错误
AttributeError:'list'对象没有属性'queryset'
。我遵循了这个指示。现在我只有两个下拉框,没有可选择的内容。你是否做了@chidimo在2018年评论中所说的所有其他事情,比如将dal和dal_select2放在所有其他应用之上?我不必做他向widgets.py建议的更改,所以请不要尝试那个。非常感谢您的帮助。我尝试过使用这个解决方案,还添加了url。但是我遇到了一个错误
AttributeError:'list'对象没有属性'queryset'
。我遵循了这个指示。现在我只有两个下拉框,没有可选择的内容。你是否做了@chidimo在2018年评论中所说的所有其他事情,比如将dal和dal_select2放在所有其他应用之上?我不必做他向widgets.py建议的更改,所以请不要尝试那个。