python - Form with dynamic select inputs, ajax stops working? -
i have form 2 select inputs, retrieve data api. select depends on first input filter list of choices. form re-renders after receiving data first input via ajax request, form retrieves filtered list of choices api.
however, after re-rerendering form. if change first select field ajax request no longer working? looking @ browser dev tools shows ajax requests no longer being generated.
original script no longer execute after ajax request, jquery script still looks valid?
views.py
@require_http_methods(['get', 'head', 'post']) def current_service_view(request): postcode = request.session.get('postcode', none) service_type = request.session.get('service_type', none) payment_method = request.session.get('payment_method', none) form = currentserviceform(postcode, fuel_type, payment_method) if request.is_ajax(): supplier_id = request.get.get('supplier', none) form = currentserviceform(postcode, service_type, payment_method, data=request.post, supplier_id=supplier_id, ) render(request, 'components/form.html', {'form': form}) if request.method == 'post': form = currentserviceform(postcode, fuel_type, payment_method, data=request.post, ) if form.is_valid(): request.session.update(form.cleaned_data) return redirect('users:usage') return render(request, 'pages/signup_current_service_functional.html', {'form': form})
forms.py
class currentserviceform(forms.form): postcode = forms.charfield(max_length=100) # may not have in end. supplier = forms.choicefield() service = forms.charfield(max_length=100) def __init__(self, postcode, service_type, payment_method, supplier_id=none, *args, **kwargs): super().__init__(*args, **kwargs) data = kwargs.get('data', none) if data: supplier_id = data.get('supplier') region = get_region(postcode) suppliers = get_suppliers(region, service_type,) supplier_choices = [('', '---- please select current supplier ----')] supplier in suppliers: supplier_name = supplier['suppliername'] supplier_choices.append((supplier['supplierid'], supplier_name)) service_choices = [('', '---- please select current tariff ----')] if supplier_id: self.fields['supplier'].initial = supplier_id services = get_supplier_services(supplier_id, region, service_type, payment_method) service in services['data']['services']: service_name = service['servicename'] service_choices.append((service_name, service_name)) self.fields['postcode'].initial = postcode self.fields['supplier'].choices = supplier_choices self.fields['service'].choices = tariff_choices # crispy form used layout below self.helper = formhelper(self) self.helper.label_class = 'sr-only' # using placeholders instead of labels self.helper.form_tag = false self.helper.layout = layout( prependedtext('postcode', '<i class="fa fa-home"></i>', placeholder="postcode", autofocus=""), field('supplier', ), field('service', type="hidden" ), )
template
{% extends "layout/focused_form_layout.html" %} {% block inner %} <p class="intro text-center">enter service details</p> <div class="row"> {% include "components/form.html" %} </div> {% endblock %} {% block inner_js %} <script defer type="text/javascript"> $('#id_supplier').change(function () { console.log($(this).val()) var supplier = $(this).val() $.ajax({ url: '{% url 'users:current_supply' %}', data: { 'supplier': supplier }, {# datatype: 'json', #} success: function (data) { $('form').replacewith(data) } }) }) </script> {% endblock inner_js %}
form.html
{% load i18n %} {% load crispy_forms_tags %} <form action="" method="post" enctype="multipart/form-data">{% csrf_token %} {% crispy form %} <input id="signup-submit" class="btn btn-block btn-cta-primary" type="submit" value="{% trans "submit" %}"/> </form>
Comments
Post a Comment