Django template - ajax response - how to?

NachoMiguel

After reading a bunch of blogs about this i can not find the answer, also searched in SO as well.

I have a template that use django template tags:

<div class="box" id="dates">
     <h2 class="h2">Dates</h2>
          <ul class="list-group">
                {% for days in dates %}
                <li class="list-group-item list-group-item-success">{{ days }}</li>
                {% endfor %}
          </ul>
</div>

This div waits for an answer that comes from an ajax response. Here is the ajax code:

$("#formi").submit(function(event){
   event.preventDefault();
    var data = new FormData($('form').get(0));

        $.ajax({
             type:"POST",
             url:"{% url 'dates' %}",
             data: data,
             processData: false,
             contentType: false,
             csrfmiddlewaretoken: '{{ csrf_token }}',

             success: function(data){
                 console.log("YEEEEEEEEEEEEEEEEAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHH")
                 $("#dates").html({{ data.dates }});
                },
        });
   });

Pretty self explainatory. It sends a form to a view and the view responds a json with the data that is going to be used to fill the for loop in the template.

I can see that the data is getting to the template in th console

enter image description here

enter image description here

But as u can see this is not recognizing my {{data.dates}} tag after $("#dates").htmlin the ajax succes

So, how can i still use this django tags in my template and get the data out of the ajax response?

Thanks in advance for any help provided.

ozgur

data is a plain text, not a python variable so you can't print it out within {{ }}. It looks like a JSON object, so you can do the following:

Assuming you have jQuery installed:

$.each(data.dates, function(i, val) {
    $('ul.list-group').empty().append(
        $('<li>').addClass('list-group-item list-group-item-success').text(val)
    )
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get values from the Ajax() into Django template?

From Dev

How to get AJAX response before sending to template using DATATABLES?

From Dev

How to convert ajax response and use for-each loop in blade template?

From Dev

How to return django form object in an AJAX request from django template?

From Dev

Django Iterating Json Response In Template

From Java

How to resolve AssertionError: .accepted_renderer not set on Response in django and ajax

From Dev

How to use django template tags on returned AJAX calls?

From Dev

How to update django template variable without reloading page (AJAX)?

From Dev

Django Ajax and Looping over Response

From Dev

ajax response not picked in django form

From Dev

Django returns invalid ajax response

From Dev

How to alert ajax response

From Dev

How to clear ajax response

From Dev

Ajax response how to access

From Dev

Rendering Jinja template in Flask following ajax response

From Dev

Respond to AJAX with Django template rendered

From Dev

Respond to AJAX with Django template rendered

From Dev

AJAX form validation in Django Template

From Dev

Return JSON response using Django template system

From Dev

Django middleware process_template_response setup

From Dev

Django REST y android (response template)

From Dev

Django pass render_to_response template in other template

From Dev

Django - Slow Ajax response seems to be delayed

From Dev

Django Ajax response date and time format

From Dev

Django, how do it in template?

From Dev

how to get $.ajax settings in response?

From Dev

How to get Ajax response in Go

From Dev

How to read AJAX response variable?

From Dev

How to handle Ajax JSON response?

Related Related

HotTag

Archive