Didn"t forge 2017-12-07 19:19 采纳率: 25%
浏览 21

POST不写入数据库

I have my requestaccess view below, which will write to my database if I remove the if request.method == 'POST', but if i keep it in. The request is never written. I'm trying to understand why the POST isn't occurring and how to make it happen?

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)

My request.method == 'POST' statements never return the correct value only the hard coded one for a test.

My template requestaccess.html can be found below. I can see the POST request for the ajax function being updated as i change the select options and when I click on the button.

{% 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 %}

Below is the console which shows the 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

After I click the submit button it gives me the following:

[07/Dec/2017 13:33:59] "POST /account/requestaccess/ HTTP/1.1" 200 3776 None None

Edit:

Changing the javascript to on('click' instead of on('change' produces the same value of None.

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2017-12-07 19:49
    关注

    checkedlist = request.GET.getlist('report_id') is the culprit. You're populating that from the GET request, and when you POST, those aren't carried over in any way.

    As a result, checkedlist is None and the save statement never fires.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测