0

I have some data in response but I want to display that data in the popup, please let me know how I can display Ajax data in the popup.

here is my views.py file...

def myview(request):
   datas=TestForm.objects.all
   template_name='test.html'
   context={'datas':datas}
   return render(request, template_name, context)

def myview(request, id):
   display=TestForm.objects.get(pk=id)
   template_name='test.html'
   context={'display':display}
   return render(request, template_name, context)

here is my html file...

{% for a in datas %}
<a href="javascript:void()" class="btn btn-primary" onclick"exampleModal({{a.id)}})" data-url="{% url 'myap:list_single' a.id %}">
  {{a.product_id}}
   </button>
  {% endfor %}

here is my popup code...where I want to dispslay AJAX data...

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria- 
  labelledby="exampleModalLabel" aria-hidden="true">
 <div class="modal-dialog" role="document">
 <div class="modal-content">
 <div class="modal-header">
  <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
   <span aria-hidden="true">&times;</span>
      </button>
      </div>
       <div class="modal-body">
         <tr>
            <td>{{datas.name}}</td>
            <td>{{datas.price}}</td>
            <td>{{datas.category}}</td>
         </tr>
        </div>
         <div class="modal-footer">
       <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
      </div>
       </div>

here is my AJAX code...

function exampleModal(id){
  $.ajax({
  url: $(this).attr("data-url")
   type: 'get',
   dataType: "HTML" 
   success: function(res) {
   $('.exampleModal').html(res);
   $("#exampleModal").modal("show");
    }
  });
  }
saini tor
  • 223
  • 6
  • 21
  • What exactly is the problem there? Does your ajax request return HTTP 200? Are you getting any error, either in Python or in browser console? Can you add it to the question? – yedpodtrzitko Sep 23 '20 at 03:34
  • you are returning html from backend ? – Swati Sep 23 '20 at 04:26
  • Consider edit question title to **How to display Ajax response data in Bootstrap4 Modal?** as popup can be confusing with popover (https://getbootstrap.com/docs/4.0/components/popovers/) and what you are using in your code is modal (https://getbootstrap.com/docs/4.0/components/modal/) – Gabriel Alejandro López López Sep 23 '20 at 04:49

1 Answers1

0
function exampleModal(id){
  $.ajax({
  url: $(this).attr("data-url")
   type: 'get',
   dataType: "HTML" 
   success: function(res) {
     $('#exampleModal .modal-body').html(res); //here is where the response is added to the body element of the modal
     $("#exampleModal").modal("show");
   }
  });
}
  • could you please guide me how I can pass `csrf_token` in Ajax, I want to use the `POST` method – saini tor Sep 23 '20 at 05:42
  • Normally the card token comes from server and you add it to the form as a hidden field. When posting, you send that value with the name of the variable the framework expects. Don't know specifics for django. But if you are obtaining the details of a product, this should be a GET request as you are not modifying the data. POST is used when you alter (create/update/delete) data – Gabriel Alejandro López López Sep 23 '20 at 05:44
  • I am passing the `POST` method in my `form` and in `AJAX` here is the `GET` method, so my form is not updating, it's giving me a `csrf_token` issue... – saini tor Sep 23 '20 at 05:46
  • Why are you using POST instead of GET? – Gabriel Alejandro López López Sep 26 '20 at 04:04