简体   繁体   中英

how to get data from different table using ajax in form of checkbox in django

Here i want to have my contactperson data in form of checkbox but using ajax call,I am able to bring in form of dropdown but after doing changes for converting it in form of checkbox its not working can anyone tell me how to change it

views.py

def add_project(request):
    error = ""
    if not request.user.is_staff:
        return redirect('admin_login')

    cpphone = ''
    cpemail = ''

    cust1 = Customer.objects.all()
    if request.method == 'POST':
        d = request.POST['customer']
        c = request.POST['contactperson']
        pn = request.POST['pname']
        pl = request.POST['plength']
        pt = request.POST['ptype']
        pc = request.POST['pcost']
        ptech = request.POST['ptechnologies']
        psd = request.POST['psdate']
        ped = request.POST['pedate']
        pdesc = request.POST['pdescription']

        d1 = Customer.objects.get(customer_id=d)
        contactperson1 = Contactperson.objects.get(person_id=c)

        cpphone = Contactperson.objects.get(person_id=c).person_phone
        cpemail = Contactperson.objects.get(person_id=c).person_email

        # print(cpphone, cpemail)

        try:
            Allproject.objects.create(customer=d1, contactperson=contactperson1, contactpersondetails=cpphone, contactpersonemail=cpemail, project_name=pn, project_length=pl, project_type=pt,
                                      project_cost=pc, project_technologies=ptech, project_startdate=psd, project_enddate=ped, project_description=pdesc)

            error = "no"
        except:
            error = "yes"

    d = {'error': error, 'cust': cust1}
    return render(request, 'projectfolder/add_project.html', d)


def load_courses(request):
    cust_id = request.GET.get('customer')
    # print(cust_id)
    proj = Contactperson.objects.filter(customer_id=cust_id)
    return render(request, 'projectfolder/courses_dropdown_list_options.html', {'proj': proj})

add_project.html

here i am only posting main fields that i want to be in form of checkbox See here i have contact person in form of select dropdown but i want that in checkbox format for each value

<form class="row g-3 my-3" method="POST" id="indexform" data-courses-url="{% url 'ajax_load_courses' %}">
  {% csrf_token %}
  <label>Customer Name</label>

  <select name="customer" id="customer" class="form-control">
    <option value="">---Select Customer----</option>

    {% for i in cust%}
    <option value="{{i.pk}}">{{i.customer_name}} [{{i.customer_id}}]</option>
    {% endfor %}
  </select>

  <label>Contact Person</label>
  <select required name="contactperson" id="contactperson" class="form-control">


  </select>
</form>

<script>
  $("#customer").change(function () {
    var url = $("#indexform").attr("data-courses-url");
    var customerId = $(this).val();
    console.log(customerId);

    $.ajax({
      url: url,
      data: {
        customer: customerId,
      },
      success: function (data) {
        $("#contactperson").html(data);
      },
    });
  });
</script>

courses_dropdown_list_options.html

<option value="">----select Contact Person-----</option>

{% for i in proj %}
<option value="{{i.pk}}">{{i.person_name}}</option>
<!-- <input type="checkbox" name="contactperson" value="{{i.person_name}}">{{i.person_name}} -->
{% endfor %}

Hy there. There are mainly two ways to communicate to the backend, Either from the method you adapted and The other is through API. (Application Programmable Interface), In case you are using Ajax. the best practice is to go for an API endpoint, You have to simply make a request, on event over The Checkbox you Mentioned. to the endpoint and it will give you a response in JSON format that you will then utilize in your own way: Popular REST Frameworks are 1, Django Rest Framework, A little hard for Beginners https://www.django-rest-framework.org/ 2: Fast API a relatively simple approach https://fastapi.tiangolo.com/ 3: Django Ninja, Simplest one i think https://django-ninja.rest-framework.com/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM