多重分页(AJAX)不适用于Django-el-pagination



我有2个QuerySets:发表和评论。我正在使用django-el-pagination使用ajax进行渲染。

这是我的观点:

def profile(request, user, extra_context=None):
    profile = Profile.objects.get(user__username=user)
    page_template = 'profile.html'
    if request.is_ajax():
        user_queryset = request.GET.get('user_queryset')
        print('Queryset:', user_queryset)
        if user_queryset == 'user_posts':
            page_template = 'user_posts.html'
        elif user_queryset == 'user_comments':
            page_template = 'user_comments.html'
        else:
            pass
    print('Template:', page_template)
    user_posts = Post.objects.filter(user=profile.user).order_by('-date')
    user_comments = Comment.objects.filter(user=profile.user).order_by('-timestamp')
    context = {'user_posts': user_posts,'user_comments': user_comments, 'page_template': page_template}
    if extra_context is not None:
        context.update(extra_context)
    return render(request, page_template, context)

我有一个Ajax调用,可以找出正在使用哪个查询集。因此,当单击"更多评论"或"更多帖子"(在模板中(以获取更多分页的对象时,我知道它来自哪个QuerySet。但是,当我使用上述代码并单击"更多"以进行AJAX分页时,它会附加整个页面,而不是相关的子模板(user_posts.htmluser_comments.html)。但是if request.is_ajax()代码块可以正常工作;它打印了正确的模板以便使用,因此不应该发生。

当我将代码块更改为此

if request.is_ajax():
    page_template = 'user_posts.html'

Post的AJAX分页作品。但是,我想为Comment添加Ajax分页。为什么我的最初if request.is_ajax()不起作用,我该如何修复?

编辑:

我单击more posts的输出:

Queryset: None
Template: profile.html
Queryset: user_posts
Template: user_posts.html

JS

$('body').on('click', '.endless_more', function() {
    console.log($(this).html()); #works successfully 
    var user_queryset;
    if ($(this).html() === 'more posts') {
        console.log('POSTS'); #works successfully 
        var user_queryset = 'user_posts'
    } else if ($(this).html() === 'more user comments') {
        user_queryset = 'user_comments';
        console.log('COMMENTS'); #works successfully 
    } else {
        console.log('none');
    }
    $.ajax({
        type: 'GET',
        url: window.location.href,
        data: {
            'user_queryset': user_queryset
        }
    })
});

profile.html

<!--posts-->
<div class="user_posts_div">
    <div class="endless_page_template">
        {% include "user_posts.html" %}
    </div>
</div>
<!--comments-->
<div class="user_comments_div">
    <div class="endless_page_template">
        {% include "user_comments.html" %}
    </div>
</div>

user_posts.html (子模板(

{% paginate 5 user_posts %}
    {% for post in user_posts %}
        <div class="user_post">
            <p class="user_post_title_p"><a class="user_post_title" href="{% url 'article' category=post.entered_category id=post.id %}">{{ post.title }}</a></p>
            <p class="user_post_category">/{{ post.entered_category }}</p>
            <p class="user_post_date">{{ post.date|timesince }}</p>
        </div>
    {% endfor %}
{% show_more 'more posts' '...' %}

下行的输出是什么?

print('Queryset:', user_queryset)

我认为您在下行中有问题

user_queryset = request.GET.get('user_queryset')

这不是返回正确的匹配条件和评论部分的匹配参数值。

您可以在附近检查javascript:

   user_queryset = 'user_comments';

尝试将其更改为:

   var user_queryset = 'user_comments';

我假设,如果您直接转到注释,则变量user_queryset将不确定,并且不会以'user_comments'的方式传递。

最新更新