Answer the question
In order to leave comments, you need to log in
Django + ajax What is the right way to reload objects?
Good day.
What is: the page on which objects are unloaded, here is its fragment (I give the section for convenience):
index.html
<section>
<!-- Latest news title -->
<div class="wrap wrap_white">
<div class="container title">
<h1 class="title__h1 underscore">Latest news</h1>
</div>
</div>
<!-- END Latest news title -->
<div class="wrap wrap_gray pt20">
<div class="container">
<div class="row">
{% for news in news_items_list %}
{% if forloop.counter == 1 %}
<div class="col-sm-6">
<div class="thumbnail thumbnail_big">
<img src="{{ news.image.url }}" height="350" width="560" alt="News">
<div class="caption thumbnail__caption">
<div class="news caption__news">
<p class="news__category yellow-line">{{ news.category }}</p>
<a href="{% url 'mainApp:detail_news' news.id %}" class="news__head">{{ news.title }}</a>
<p class="news__desc"> {{ news.content | truncatechars:360 }}</p>
</div>
<div class="posted">
<span class="posted__date"><i class="icon-clock-1"></i> {{ news.date }}</span>
<ul class="posted__icon">
<li>
<span>
<i class="icon-comment-empty"></i>29
</span>
</li>
<li>
<span>
<i class="icon-eye"></i>2.3k
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
{% else %}
{% include 'mainApp/inc-news_block-sm3.html' %}
{% endif %}
{% endfor %}
</div>
</div>
<!-- Btn load-->
<div class="ajax_load">
<i class="icon-arrows-cw"></i>Load more
<svg width="128" height="40" viewBox="0 0 128 40" xmlns="http://www.w3.org/2000/svg">
<rect x='0' y='0' fill='none' width='128' height='40'></rect>
</svg>
</div>
<!-- END Btn load-->
</div>
<!-- /Container-->
</section>
<div class="col-sm-3">
<div class="thumbnail thumbnail_small">
<a href="{% url 'mainApp:detail_news' news.id %}" class="thumbnail__link">
<img src="{{ news.image.url }}" height="153" width="270" alt="News">
</a>
<div class="caption thumbnail__caption">
<div class="news caption__news">
<p class="news__category yellow-line">{{ news.category }}</p>
<a href="{% url 'mainApp:detail_news' news.id %}" class="news__link">
<p class="news__text">{{ news.title | truncatechars:63}}</p>
</a>
</div>
<div class="posted">
<span class="posted__date"><i class="icon-clock-1"></i>{{ news.date }}</span>
<ul class="posted__icon">
<li>
<span>
<i class="icon-comment-empty"></i>11
</span>
</li>
<li>
<span>
<i class="icon-eye"></i>1.1k
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
def more_news(request):
tmp = IndexView.count_news
if request.is_ajax():
objects = News.objects.order_by('-date')[tmp:tmp + 4]
html = loader.render_to_string('mainApp/inc-news_block-sm3.html', {'news': objects}, request=request)
data = {'errors': False, 'html': html}
try:
tmp += 4
return JsonResponse(data)
except News.DoesNotExist:
return JsonResponse({'errors': 'Something happened'})
raise Http404
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