S
S
Sergey08082017-10-09 14:33:03
HTML
Sergey0808, 2017-10-09 14:33:03

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

1 answer(s)
V
Viktor, 2017-10-20
@Sergey0808

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})

_list.html template code:
<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>

Template code _list_ajax.html:
{% 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 %}

Code js:
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);
            }
        });
    }
});

"Lazy load" implemented as pagination made on django. On the first request, the main template is rendered with _list_ajax.html included, to which the data that needs to be rendered is passed. When you click the "More" button, a get ajax request is sent with the number of the next page - if it is, then the rendered template _list_ajax.html comes in the response - which is inserted into the corresponding block.
Hope it helps :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question