2017-12-07 14 views
0

以下のrequestaccessビューがあります。これは、request.method == 'POST'を削除するとデータベースに書き込まれますが、そのままにしておくとリクエストは書き込まれません。 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/12月/ 2017年午後01時33分59秒] "POST /アカウント/ requestaccess/HTTP/1.1" 200 3776 なし なし

編集:

javascriptをonに変更する( 'change'の代わりに 'click'を指定すると、Noneと同じ値が生成されます。

答えて

3

checkedlist = request.GET.getlist('report_id')が原因です。あなたはGETリクエストからそれを設定しています。あなたがPOSTするとき、それらは何らかの方法で持ち越されません。

結果として、checkedlistNoneであり、saveステートメントは実行されません。

+0

GETリクエストを同じビューのPOSTに送信するにはどうすればよいですか?もっとアヤックス? – student101

+0

GETリクエストをどのように使用しているのか、あなたの質問からは不明ですが、あなたのフォームを送信するPOST要求の一部である必要があります。 (したがって、フォームの設定を事前入力するためにGETリクエストを使用し、ユーザーがフォーム上で選択したものを投稿してください) –

+0

私のGETリクエストは、Profileというページから来ます。チェックボックスをオンにします。 requestaccessのURLにはreport_id =が含まれており、レポート名は事前に選択されたチェックボックスでテンプレートに渡されます。その後、ユーザーはいくつかの他のオプションを変更してから、上記で定義したPOST要求が

student101

関連する問題