Im trying to implement auocomplete in my django project.
I want to pass a parameter through the url in the form. This is my Form
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']
my url pattern is
url(r'^(?P<playlist_id>[0-9]+)/create_song/song-autocomplete/$', views.SongAutocomplete.as_view(), name='song-autocomplete', ),
so while calling the url song-autocomplete it needs a parameter playist_id. How do I send it using the url='login:song-autocomplete'?
Thank you.
full example using my own code:
forms.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',)
urls.py
url(
'^relationships/RelationshipAutoUrl/(?P<type>\w+)/$',
RelationshipAutoView.as_view(),
name='PersonAutoUrl',
),
views.py
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
initiating form and passing kwarg type in view:
def relationships(request):
relationships = ['Friend', 'Family','Business']
forms = {}
for key in relationships:
forms[key] = AddRelationshipForm(request.POST or None, prefix='new'+key, type=key)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.