简体   繁体   English

如何使用 ajax 以 django 中的复选框形式从不同的表中获取数据

[英]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在这里,我想以复选框的形式获取我的联系人数据,但使用 ajax 调用,我能够以下拉列表的形式引入,但是在进行更改以将其转换为复选框的形式后,它不起作用任何人都可以告诉我如何更改它

views.py视图.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 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在这里,我仅以复选框的形式发布我想要的主要字段参见此处,我有联系人以 select 下拉列表的形式,但我希望每个值都以复选框格式

<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 course_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.与后端通信主要有两种方式,一种是您适应的方法,另一种是通过 API。 (Application Programmable Interface), In case you are using Ajax. (应用程序可编程接口),如果您使用的是 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.最佳实践是 go 用于 API 端点,您只需在您提到的复选框上的事件上提出请求。 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/ 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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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