我正在尝试使用django-selectable。
model.py :
class Country(models.Model):
name = models.CharField(verbose_name='Страна', max_length=128, blank=True)
def __str__(self):
return self.name
class Region(models.Model):
country = models.ForeignKey(Country, verbose_name='Город')
name = models.CharField(verbose_name='Город', max_length=128, blank=True)
def __str__(self):
return self.name
class City(models.Model):
country = models.ForeignKey(Country, verbose_name='Город')
region = models.ForeignKey(Region, verbose_name='Регион')
name = models.CharField(verbose_name='Город', max_length=128, blank=True)
def __str__(self):
return self.name
Lookup.py :
from subscribers.models import City
from selectable.base import ModelLookup
from selectable.registry import registry
class CityLookup(ModelLookup):
model = City
search_fields = ('name__icontains', )
registry.register(CityLookup)
forms.py :
import selectable.forms as selectable
from subscribers.models import City
from partners.lookup import CityLookup
class ShowSomeForm(forms.Form):
autocomplete = forms.CharField(
label='Type the name of a fruit (AutoCompleteWidget)',
widget=selectable.AutoCompleteWidget(CityLookup),
)
view.py
...
form = ShowSomeForm()
return render(request, 'personal_accout_partners.html', {'formPart': formPartners, 'formMes': formMessages, 'form': form})
personal_accout_partners.html
{{form}}
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/
1.8.13/themes/ui-lightness/jquery-ui.css" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/
jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/
jqueryui/1.8.13/jquery-ui.min.js"></script>
但是,此代码不起作用(在//selectable/partners-citylookup/
我有这个):
{"data": [{"id": 3161, "label": "Акташ", "value": "Акташ"}, {"id": 3162, "label": "Акутиха", "value": "Акутиха"}, {"id": 3163, "label": "Алейск", "value": "Алейск"}, {"id": 3164, "label": "Алтайский", "value": "Алтайский"}, {"id": 3165, "label": "Баево", "value": "Баево"}, {"id": 3166, "label": "Барнаул", "value": "Барнаул"}, {"id": 3167, "label": "Белово", "value": "Белово"}, {"id": 3168, "label": "Белокуриха", "value": "Белокуриха"}, {"id": 3169, "label": "Белоярск", "value": "Белоярск"}, {"id": 3170, "label": "Бийск", "value": "Бийск"}, {"id": 3171, "label": "Благовещенск", "value": "Благовещенск"}, {"id": 3172, "label": "Боровлянка", "value": "Боровлянка"},
我该怎么办这个问题?