我尝试为django2.2配置 autocomplete_light ,但是我遇到了一些错误,因为我想是django版本。最初与
render() got an unexpected keyword argument 'renderer'
通过在renderer=None
的渲染函数中添加widgets.py
来解决。之后,发生此错误,from django.core import urlresolvers its depreciated in django 2.2
。所以我用了from django.urls import reverse
(在base.py中)。然后错误更改为function' object has no attribute 'NoReverseMatch'
。
autocomplete_light_registry.py
from autocomplete_light import shortcuts as autocomplete_light
from django.db.models import Q
from assets.models import Dealer
class DealerAutocomplete(autocomplete_light.AutocompleteModelBase):
autocomplete_js_attributes = {
'placeholder' : 'Region'
}
attrs={
'placeholder':'Region',
'class':'form-control',
'data-autocomplete-minimum-characters': 1,
}
def choices_for_request(self):
q = self.request.GET.get('q','')
choices = self.choices.filter(is_deleted=False)
if q:
choices = choices.filter(Q(region__istartswith=q))
return self.order_choices(choices)[0:self.limit_choices]
autocomplete_light.register(Dealer, DealerAutocomplete)
forms.py
class DealerForm(forms.ModelForm):
class Meta:
model = Dealer
fields = ('region','dealer_name','type_dealer','address','loc_latitude','loc_longitude','mobile','phone','email')
widgets = {
"region": autocomplete_light.ChoiceWidget('DealerAutocomplete'),
'dealer_name' : forms.TextInput(attrs={'class':'form-control', 'placeholder':'Dealer name'}),
'type_dealer' : forms.Select(attrs={'class':'form-control', 'placeholder':'Dealer Type'}),
'address' : forms.Textarea(attrs={'class':'form-control', 'placeholder':'Address'}),
'loc_latitude' : forms.TextInput(attrs={'class':'form-control', 'placeholder':'Location Latitude'}),
'loc_longitude' : forms.TextInput(attrs={'class':'form-control', 'placeholder':'Location Longitude'}),
'mobile' : forms.TextInput(attrs={'class':'form-control', 'placeholder':'Mobile'}),
'phone' : forms.TextInput(attrs={'class':'form-control', 'placeholder':'Phone'}),
'email' : forms.EmailInput(attrs={'class':'form-control', 'placeholder':'Email'})
}
我想知道autocomplete_light是否在django2.2中工作,或者我的代码中是否存在任何问题。请更正或指出错误。