Answer the question
In order to leave comments, you need to log in
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 %}
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)
$('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>')
}
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question