问题
I am using jQuery autocomplete search (shown below):
def CitySearch(request):
country_search_id = <country_id> # "country_id" to be sent from html page (value stored in a field)
if request.is_ajax():
q = request.GET.get('term','')
names = City.objects.filter(country__id=country_search_id).filter(name__icontains=q).annotate(value=F('name'), label=F('name')).values('id', 'value', 'label')
result = list(names)
data = json.dumps(result)
mimetype = 'application/json'
return HttpResponse(data, mimetype)
The first filter (.filter(country__id=country_search_id)
) is intended for filtering out all countries other than the one selected in the country autocomplete field (using variable country_search_id
).
Is it possible to send the captured country id back to the CitySearch view so as to be able to apply the filter? The country id value is stored in a form field on the page.
Update
forms.py
class CreateZeeForm(forms.ModelForm):
class Meta:
model = Supplier
fields = ('name', 'country', 'city', ...)
来源:https://stackoverflow.com/questions/59758100/how-to-send-a-variable-from-html-page-back-to-django-view-for-further-processing