How to send a variable from html page back to Django view for further processing

江枫思渺然 提交于 2020-01-25 07:32:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!