Home POST doesn't write to database
Reply: 1

POST doesn't write to database

student101
1#
student101 Published in 2017-12-07 19:19:44Z

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.

Benjamin Hicks
2#
Benjamin Hicks Reply to 2017-12-07 19:49:55Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302719 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO