S
S
Sergey08082017-10-10 13:03:01
JavaScript
Sergey0808, 2017-10-10 13:03:01

Python Django, how to parse incoming JSON response into html?

Good afternoon, I send an ajax request to views and I need to parse this data into html, that is, let's say the following template:

<div class="block-news__list">
      {% for item in news_list %}
        <div class="block-news-item">
          <div class="block-news-item__thumb">
            <a href="{% url 'news:detail' item.slug %}">
              {% if item.image_preview %}
                <img src="{% thumbnail item.image_preview 294x190 crop %}" alt="{{ item.image_alt }}">
              {% endif %}
            </a>
          </div>
{% endfor %}

That is, in place of {{ item.image_alt }}
Views:
def pagination_ajax(request):
        if request.is_ajax():
            news_response = {}
            query = request.GET.get('value', 12)
            news = (News.objects.all()[query:])
            news_response['image'] = list(news.values_list('image_preview'))
            news_response['slug'] = list(news.values_list('slug'))
            news_response['title'] = list(news.values_list('title'))
            return JsonResponse(json.dumps(news_response, ensure_ascii=False), content_type='application/json', safe=False)

js:
$('button.pagination').click(function () {
    $.ajax({
        type: 'GET',
        url: 'http://127.0.0.1:8000/news/ajax/paginate',
        dataType: 'json',
        success: function (data) {
            $('.ajax-data').append(data + '</br>')

        }


    });
});

Object json part:
"image": [["detryewry.jpg"], ["children_and_principal.jpg"], ["anti-crisis_preview.JPG"], ["kazakhstan.jpg"], ["gM6QJ51-MWM.jpg"] , ["0868823079a26cb88b65a6b73a37207e.jpg"], ["anti-crisis_preview_OeehSfW.JPG"], ["camp.jpg"], ["fc9d6b6f1c4b5e0d95c3c07e266e6bf9.jpg"]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question