ajax get request query

Submitted 5 years ago
Ticket #25
Views 437
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 5 years ago

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

Submitted 5 years ago

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

- bhavana 5 years ago

year_id in views undefined variable ??

- sarojkoirala42 5 years 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 5 years ago

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

- sarojkoirala42 5 years ago

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

- sarojkoirala42 5 years ago

i am using get request//

- sarojkoirala42 5 years 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 5 years 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 5 years ago

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

- bhavana 5 years ago

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

- sarojkoirala42 5 years 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 5 years ago

One correction change to Jsonresponse

- bhavana 5 years ago

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

- sarojkoirala42 5 years ago

Yes that you have to take care in ajax event

- bhavana 5 years ago

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

- sarojkoirala42 5 years 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 5 years ago

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

- sarojkoirala42 5 years ago

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

- bhavana 5 years ago


Latest Blogs