POST不会写入数据库

时间:2017-12-07 19:19:44

标签: ajax django django-views

我有下面的requestaccess视图,如果我删除了if request.method ==' POST',如果我保留它,将写入我的数据库。请求永远不会被写入。我试图理解为什么POST没有发生以及如何实现它?

def requestaccess(request):

    owner = User.objects.get (formattedusername=request.user.formattedusername)

    reportdetail = QVReportAccess.objects.filter(ntname = owner.formattedusername, active = 1).values('report_name_sc')
    reportIds = QVReportAccess.objects.filter(ntname = owner.formattedusername).values_list('report_id', flat=True)
    checkedlist = request.GET.getlist('report_id')
    reportlist = QvReportList.objects.filter(report_id__in= checkedlist, active = 1).values_list('report_name_sc',flat = True)


    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 = '7'
    if request.method == 'POST':
        selectedaccesslevel = request.POST.get('accesslevelid')
        print(selectedaccesslevel)
    selectedphi = '0'
    if request.method == 'POST':
        selectedphi = request.POST.get('phi')
        print(selectedphi)
    if request.method == 'POST':
        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()

    args = {'retreivecheckbox': reportlist, 'cfo7':facilitycfo, 'cfo5':divisioncfo, 'checkedlist':checkedlist }

    return render(request,'accounts/requestaccess.html', args)
#    return render(request, 'accounts/requestaccess.html', args)

我的request.method ==' POST'语句永远不会返回正确的值,仅用于测试的硬编码值。

我的模板requestaccess.html可以在下面找到。当我更改选择选项并单击按钮时,我可以看到更新ajax函数的POST请求。

{% extends 'base.html' %}

  {% block head %}
  <title> Access Request Form </title>
  {% endblock %}

  {% block body %}

    <div class="container">
      <div class="row">
        <div class="col">
          <h2>{{ user.username }}</h2></br>
          <ul>
            <li>Employee First Name: {{ user.first_name }}</li>
            <li>Employee Last Name: {{ user.last_name }}</li>
            <li>Coid: {{ user.coid }}</li>
            <li>Facility: {{ user.facility }}</li>
            <li>Title: {{user.title}}</li>

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

  <form action = "{% url 'requestaccess' %}" form method = "POST">
    {% csrf_token %}
          <h2>Applications:</h3></br>

            {% for app in retreivecheckbox %}
            <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" checked> {{ app }}
              </li>
            {% endfor %}
        </div>

      </div>


      <div class="row">
        <div class="col">
          <label for="accesslevel"><h3>Access Level</h3></label>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<select title ="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" title = "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> </br>

  <button href="{% url 'submitted' %}" class="btn btn-primary my_select" type = "submit"> Submit </button>
              <script>
          $(document).ready(function () {
              $('.my_select').on('change', function () {
                  var phi = $('#phi').val();
                  var accesslevelid = $('#accesslevelid ').val();
                  $.ajax({ url: "{% url 'requestaccess' %}",
                          headers: { 'X-CSRFToken': '{{ csrf_token }}' },
                          data: {
                            phi: phi,
                            accesslevelid: accesslevelid,
                          },
                          type: 'POST',
                          success: function (result) {
                            ;
                          },
                        });
                });
            });
          </script>
</div>


  </form>

  {% endblock %}

以下是显示POST的控制台:

[07/Dec/2017 13:21:14] "GET /account/requestaccess/?report_id=84&report_id=87&report_id=88 HTTP/1.
3

[07/Dec/2017 13:21:22] "POST /account/requestaccess/ HTTP/1.1" 200 3928
3
1

点击提交按钮后,它给出了以下内容:

[07 / Dec / 2017 13:33:59]&#34; POST / account / requestaccess / HTTP / 1.1&#34; 200 3776 没有 无

编辑:

将javascript更改为开启(&#39;点击&#39;而不是on(&#39;更改&#39;生成相同的无值。

1 个答案:

答案 0 :(得分:3)

checkedlist = request.GET.getlist('report_id')是罪魁祸首。您正在从GET请求填充该内容,当您发布POST时,这些内容不会以任何方式结转。

因此,checkedlistNone,并且保存语句永远不会触发。