ajax get request query

Submitted 3 years, 10 months ago
Ticket #25
Views 269
Language/Framework Django
Priority High
Status Closed

Views.py 

def updatetable(request):

   if request.method == 'GET' :

    post_id = p_board_detail.objects.filter(board_year_id= year_id)

    return HttpResponse("post_id")

Submitted on Jun 18, 20
add a comment

2 Answers

Acknowledged. May i know what is the issue?

Submitted 3 years, 10 months ago

here is the code for get data from db with the views function in django but not getting data....

Submitted 3 years, 10 months ago

Use JsonResponseData method instead of HttpResponse. After that success function in ajax. check console.log(data) whether its returning data or not.

- bhavana 3 years, 10 months ago

year_id in views undefined variable ??

- sarojkoirala42 3 years, 10 months ago

You need to understand 2 things, whether your request is GET or POST
If its GET request then you need to handle the variable logic in your view and just return the data to AJAX
If you want to pass the data from AJAX then its POST request

- bhavana 3 years, 10 months ago

year_id in views geting undefined variable ?? from django.http import JsonResponse is for jsonresponsedata

- sarojkoirala42 3 years, 10 months ago

year_id in views geting undefined variable ?? from django.http import JsonResponse is for jsonresponsedata

- sarojkoirala42 3 years, 10 months ago

i am using get request//

- sarojkoirala42 3 years, 10 months ago

def updatetable(request): if request.method == 'GET' : post_id = p_board_detail.objects.filter(board_year_id= year_id) return JsonResponseData(post_id)

- sarojkoirala42 3 years, 10 months ago

<select name="bordlist"> <option class="mySelect">--Select Board Year--</option> {%for year in board_list %} <option value="{{year.id}}">{{year.board_year}}</option> {%endfor%} </select>

      <hr>
        <script>
         $(document).ready(function(){

            $(".mySelect").change(function(){

                $.ajax( 
                 { 
                  type:"GET", 
                  url: "{% url 'updatetable' %}",
                  data:{ year_id : $(this).val()},

                success: function( data ) 
                  { 
                  console.log(data) ;
                  $("#loadtable").html(data);

                   }
                });
         }) ;
          });
        </script>


    <div class="table-responsive" >
        <table class="table table-striped">
            <thead class="thead-dark">
            <tr>
                <th scope="col">S.N</th>
                <th scope="col">Name</th>
                <th scope="col">Post</th>
            </tr>
            </thead>
            <tbody id="loadtable" >
                {%for dt in data %}
            <tr>
                <th scope="row">{{ forloop.counter }}</th>
                <td>{{dt.name}}</td>
                <td>{{dt.post}}</td>
            </tr>
               {%endfor%}
            </tbody>
        </table>
</div>
- sarojkoirala42 3 years, 10 months ago

sorry , I dont understand what is the issue ?.. are you taking about issue in views or AJAX?

- bhavana 3 years, 10 months ago

can you plz provide me the sample to get data on ajax views.py ajax method on html

- sarojkoirala42 3 years, 10 months ago

check this
AJAX $.ajax({ url: "ajax.aspx", type: "get", //send it through get method data: {

    year_id: 4, 

  },
  success: function(response) {
    //Do Something
  },
  error: function(xhr) {
    //Do Something to handle error
  }
});

View
def updatetable(request,year_id):

   if request.method == 'GET' :

    post_id = p_board_detail.objects.filter(board_year_id= year_id)

    return HttpResponse("post_id")
- bhavana 3 years, 10 months ago

One correction change to Jsonresponse

- bhavana 3 years, 10 months ago

are we getting dropdown event on change function calling in jquery? by looking on code

- sarojkoirala42 3 years, 10 months ago

Yes that you have to take care in ajax event

- bhavana 3 years, 10 months ago

$("#make").click(function(){ console.log("Saroj") ; }); on click it would print on console or not?

- sarojkoirala42 3 years, 10 months ago

Looks like your initial issue has been resolved.now it's new requirement you are looking for it. Can you pls close this ticket and submit new one

- bhavana 3 years, 10 months ago

I am stuck on the same probelm but trying to resolved ..thanks for. Ur support..

- sarojkoirala42 3 years, 10 months ago

Ajax Get request issue is completed. Now you are looking for onclick event. Please close the ticket.

- bhavana 3 years, 10 months ago


Latest Blogs