Answer the question
In order to leave comments, you need to log in
Python Django, how to make show more + ajax instead of pagination?
Views:
def pagination_ajax(request):
if request.is_ajax():
elem = request.GET.get('value', 10)
news = serializers.serialize('json', News.objects.all()[:elem])
return HttpResponse(news, content_type='application/json')
<code>
url:
url(r'^ajax/paginate$', views.pagination_ajax, name='paginate_ajax')
Button:
<button type="submit" id="pagination">Показать больше</button>
JS:
$('#pagination').click(function () {
$.ajax({
type: 'get',
url: 'http://127.0.0.1:8000/news/ajax/paginate',
dataType: "json"
});
});
Как исправить чтоб работало?
Answer the question
In order to leave comments, you need to log in
Something like this:
View code:
def projects_list_view(request):
projects_per_page = 9
projects = Project.objects.language(request.LANGUAGE_CODE).all().order_by('order').select_related('customer')
paginator = Paginator(projects, projects_per_page)
page = request.GET.get('page')
try:
projects = paginator.page(page)
except PageNotAnInteger:
projects = paginator.page(1)
except EmptyPage:
if request.is_ajax():
return HttpResponse('')
projects = paginator.page(paginator.num_pages)
if request.is_ajax():
return render(request, 'projects/_list_ajax.html', {'projects': projects})
return render(request, 'projects/_list.html', {'projects': projects, 'seo': seo,
'page_description': page_description})
<div class="row" id="project_list">
{% include 'projects/project_list_ajax.html' %}
</div>
<div class="row" id="preloader">
.....
</div>
<div class="row">
<div class="col m12 s12 center-align">
<button class="btn waves-effect waves-light" id="lazy_more">
{% trans 'More projects' %}
</button>
</div>
</div>
{% for project in projects %}
<div class="col m4 s12">
<div class="card large sticky-action">
<div class="card-image waves-effect waves-block waves-light">
{% if project.image %}
<img src="{{ project.image.url }}" class="activator" alt="{{ project.title }}">
{% endif %}
</div>
<div class="card-content">
<h4 class="card-title activator">
{{ project.title }}
<i class="material-icons right">more_vert</i>
</h4>
<div class="card-customer">
<h6>{{ project.customer.title|upper }}</h6>
<span>{{ project.customer.description }}</span>
</div>
</div>
<div class="card-reveal">
<h4 class="card-title">
{{ project.title }}
<i class="material-icons right">close</i>
</h4>
{{ project.description }}
</div>
</div>
</div>
{% endfor %}
var page = 1;
var empty_page = false;
var block_request = false;
$('#lazy_more').click(function (e) {
e.preventDefault();
if (empty_page === false && block_request === false) {
block_request = true;
page += 1;
$('#preloader').show();
$.get('?page=' + page, function (data) {
$('#preloader').hide();
if (data === '') {
empty_page = true;
} else {
block_request = false;
$('#project_list').append(data);
}
});
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question