2017-12-07 5 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 요청을 볼 수 있습니다.

[07/DEC/2017 13시 33분 59초 :

[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 

후 나는 그것이 다음 나에게주는 버튼 제출 클릭 : 아래

{% 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를 보여줍니다 콘솔입니다 ] "POST/계정/requestaccess/HTTP/1.1"200 3776 없음 없음

편집 :

자바 스크립트를 켜기로 변경하면 ('변경'을 누르는 대신 '클릭'하면 같은 값인 '없음'이 생성됩니다.

답변

3

checkedlist = request.GET.getlist('report_id')은 범인입니다. 당신은 GET 요청으로부터 그것을 채우고 있으며 POST 할 때 어떤 식 으로든 이월되지 않습니다.

결과적으로 checkedlistNone이고 save 문은 실행되지 않습니다.

+0

GET 요청을 동일한보기의 POST에 어떻게 보내면 되나요? 더 아약스? – student101

+0

GET 요청을 사용하는 방법에 대한 질문은 명확하지 않지만 양식을 제출하는 POST 요청에 포함되어 있어야합니다. (그래서 한 가지 방법은 GET 요청을 사용하여 양식의 설정을 미리 채운 다음 사용자가 양식에서 선택하는 다른 항목으로 게시하는 것입니다.) –

+0

내 GET 요청은 프로필이라는 페이지에서 가져옵니다. 여기서 사용자는 확인란을 선택합니다. requestaccess의 URL에는 report_id =가 포함되며 보고서 이름은 미리 선택된 확인란으로 템플릿에 전달됩니다. 그런 다음 사용자는 다른 옵션을 변경 한 다음 위의

를 사용하여 위에 정의 된 POST 요청이 발생해야하는 제출을 푸시합니다. – student101