我试图在我的django项目中实现auocomplete。
我想通过表单中的url传递参数。这是我的表格
class SongForm(forms.ModelForm):
song_title = forms.ModelChoiceField(
queryset=Snr.objects.all(),
widget=autocomplete.ModelSelect2(url='login:song-autocomplete',attrs={'data-placeholder': 'Autocomplete ...',
'data-minimum-input-length': 3,},)
)
class Meta:
model = Song
fields = ['song_title']
我的网址格式是
url(r'^(?P<playlist_id>[0-9]+)/create_song/song-autocomplete/$', views.SongAutocomplete.as_view(), name='song-autocomplete', ),
所以在调用url song-autocomplete时,它需要一个参数playist_id。如何使用url ='login:song-autocomplete'发送它?
谢谢。
答案 0 :(得分:0)
使用我自己的代码的完整示例:
forms.py
this.setState({
data: {
count:newCount
dataArray: newArray
}
});
urls.py
class AddRelationshipForm(autocomplete.FutureModelForm):
first = autocomplete.QuerySetSequenceModelField(
queryset=autocomplete.QuerySetSequence(Person.objects.all()),
required=False,
widget=autocomplete.QuerySetSequenceSelect2(
url='PersonAutoUrl', attrs={
'style':'width:15em;', 'data-placeholder': 'find'
}
),
)
def __init__(self, *args, **kwargs):
type = kwargs.pop('type')
super(AddRelationshipForm, self).__init__(*args, **kwargs)
self.fields['first'] = autocomplete.QuerySetSequenceModelField(
queryset=autocomplete.QuerySetSequence(Person.objects.all()),
required=False,
widget=autocomplete.QuerySetSequenceSelect2(
url='PersonAutoUrl/'+type, attrs={
'style':'width:15em;', 'data-placeholder': 'search'
}
),
)
class Meta:
model = Relation
fields = ('first',)
views.py
url(
'^relationships/RelationshipAutoUrl/(?P<type>\w+)/$',
RelationshipAutoView.as_view(),
name='PersonAutoUrl',
),
启动表单并在视图中传递kwarg 类型:
class RelationshipAutoView(Select2QuerySetSequenceView):
def get_queryset(self):
excluded = Person.objects.filter(first__owner=self.request.user, first__type=self.kwargs['type'])
queries = Person.objects.exclude(id__in=excluded)
if self.q:
queries = queries.filter(first_name__icontains=self.q)
return queries