为什么我的核对清单不会作为POST发送

时间:2017-12-12 20:47:04

标签: ajax django

我有以下模板,我的按钮用于将我的选择选项发送到POST然后切换URL。但是,我有一组用户选择的复选框,我想捕获每个复选框的report_id。我正在为我班级的项目的最后一步做很少的指导,并努力使这个表格按预期工作。

<form action = "{% url 'submitted' %}" form method = "POST">
      {% csrf_token %}
      {{ form.as_p}}
  <div class="container">
  <div class="row">
    <div class="col">
      <label for="accesslevel"><h3>Access Level</h3></label>

<select name ="accesslevelid" class="form-control my_select" id="accesslevelid">
        <option value=""> Please select your access level  </option>
        <option value="7"> Facility  </option>
        <option value="5"> Division  </option>
        <option value = "3"> Corporate  </option>
        <option value = "6"> Market  </option>
        <option value = "4"> Group  </option>
</select>

    </div>
    <div class="col">


      <label for="phi"><h3>PHI</h3></label>

      <select class="form-control my_select" id="phi" name = "phi" >
        <option value = ""> Please select if you need access to PHI data </option>
        <option value = "0"> No  </option>
        <option value = "1"> Yes  </option>

      </select>

    </div>
  </div>

    <div class="row">
<div class="container">
<div class="row">
  <div class="col">
</br>

</div>

</div>


<div class = "container">

  <div class="jumbotron">
    <div class="container">
      <h1 class="jumbotron-heading">Available Application List</h1></br>
    </div>
        <div class="container">

          <div class="row">
              <div class="col">

{% if fingrouplist is not None %}
                <h3>Financial</h3>

                        <ul>
                        {% for app in fingrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
              </div>
              <div class="col">
{% if cagrouplist is not None %}
                <h3>Care Assure</h3>
                <div class = "row">

                        <ul>
                        {% for app in cagrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
                </div>

              <div class = "row">
{% if pigrouplist is not None %}
                <h3>Performance Improvement</h3>

                        <ul>
                        {% for app in pigrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
              </div>

              <div class = "row">
{% if scgrouplist is not None %}
                <h3>Supply Chain</h3>

                        <ul>
                        {% for app in scgrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
              </div>

              <div class = "row">
{% if dssgrouplist is not None %}
                <h3>DSS Monitoring</h3>

                        <ul>
                        {% for app in dssgrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
              </div>

              <div class = "row">
{% if othgrouplist is not None %}
                <h3>Other DSS Applications</h3>

                        <ul>
                        {% for app in othgrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
              </div>

            </div>
              <div class="col">
                <div class="row">
{% if bhgrouplist is not None %}
                <h3>Behavior Health / Rehab</h3>

                        <ul>
                        {% for app in bhgrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
                </div>
                <div class="row">
{% if cegrouplist is not None %}
                <h3>Clinical Excellence</h3>
                        <ul>
                        {% for app in cegrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
                </div>
                <div class="row">
{% if psggrouplist is not None %}
                <h3>Physician Service Group</h3>

                        <ul>
                        {% for app in psggrouplist %}
                        <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                        {% endfor %}
                        </ul>
{% endif %}
                </div>

              </div>


          </div class="row">
{% if showbutton is not None %}
            </br></br>


          </div>
          <button class="btn btn-primary my_select" type="submit">Request Access</button>
          <script>
$(document).ready(function () {
              $('form').on('submit', function (e) {
                  e.preventDefault();
                  var phi = $('#phi').val();
                  var accesslevelid = $('#accesslevelid').val();
                  $.ajax({
                      url: "{% url 'submitted' %}",
                      headers: { 'X-CSRFToken': '{{ csrf_token }}' },
                      data: {
                          phi: phi,
                          accesslevelid: accesslevelid,
                        },
                      type: 'POST',
                      success: function (result) {
                            if (result.is_success) {
                              document.location = "{% url 'submitted' %}"; //
                            }
                          },
                    });
                });
            });
          </script>
{% elif showbutton is None %}
You currently have acccess to all reports nothing is available to request.
{% endif %}

</form>

我对提交的请求的看法定义如下:

def submitted(request):
    owner = User.objects.get (formattedusername=request.user.formattedusername)
    checkedlist = request.POST.getlist('report_id')
    print (f"checkedlist on submitted:{checkedlist}")

    coid = User.objects.filter(coid = request.user.coid).filter(formattedusername=request.user.formattedusername)
    facilitycfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 1).values_list('cfo_ntname', flat = True)
    divisioncfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 2).values_list('cfo_ntname', flat = True)

    selectedaccesslevel = request.POST.get('accesslevelid')
    print (f"accesslevel:{selectedaccesslevel}")

    selectedphi = request.POST.get('phi')
    print (f"phi:{selectedphi}")

    for i in checkedlist:
        requestsave = QVFormAccessRequest(ntname = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title
                                      ,report_id = i, accesslevel_id = selectedaccesslevel, phi = selectedphi , access_beg_date = '2017-01-01 00:00:00', access_end_date = '2017-01-31 00:00:00')
        requestsave.save()

    return JsonResponse({'is_success':True})

我试图打印核对表列表,但它在我的控制台中一直显示为空集。我不确定为什么,我已经尝试过GET和POST。此外,当使用JsonResponse在ajax中重定向我的URL时,它也会发送一个GET。当我查看我的控制台时,它显示如下:

[12/Dec/2017 14:37:16] "GET /account/profile/ HTTP/1.1" 200 15804
checkedlist on submitted:[]
accesslevel:3
phi:1
[12/Dec/2017 14:37:21] "POST /account/submitted/ HTTP/1.1" 200 20
checkedlist on submitted:[]
accesslevel:None
phi:None
[12/Dec/2017 14:37:21] "GET /account/submitted/ HTTP/1.1" 200 20

1 个答案:

答案 0 :(得分:1)

您可以纠正一些细节。第一个是:在你的views.py中插入以下条件:

if request.method == 'POST':
  alldata=request.POST
  # To get a specific attribute.
  data = [alldata.get('atribute_name,0)]

二。至于我自己的经验。我喜欢在我的视图(服务器)中提交表单之后处理不在客户端上的操作。原因是我有过某些浏览器和非常特殊的经历。在表单提交到我的视图之前,重定向发生。并且没有发布数据。因此,在视图结束时,只需实现所需的渲染或重定向功能。

最后,您不需要HTML中方法之前的表单。只要明白这样:

<form action = "." method = "POST">
相关问题