【问题标题】:Multiple pagination (ajax) not working for django-el-pagination多分页(ajax)不适用于 django-el-pagination
【发布时间】:2017-11-27 14:28:10
【问题描述】:

我有 2 个查询集:发布和评论。我正在使用 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 调用来找出正在使用的查询集。因此,当单击“更多 cmets”或“更多帖子”(在模板中)以获取更多分页对象时,我知道它来自哪个查询集。 但是,当我使用上面的代码并单击 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' '...' %}

【问题讨论】:

    标签: jquery python ajax django pagination


    【解决方案1】:

    下面一行的输出是什么?

    print('Queryset:', user_queryset)
    

    我认为你在下面一行有问题

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

    这没有返回正确的获取参数值以匹配帖子和评论部分的条件。

    【讨论】:

    • 是的,你是对的,肯定是这样。由于某种原因,它打印了两次,第一次打印父模板(profile.html),这是错误的;它应该只打印子模板 (user_posts.html) 知道为什么会这样做吗?我在编辑中添加了输出。
    • 两次调用意味着您在调用此视图时在 js 中做错了。您在 is_ajax() 方法之前定义了 page_template ='profile.html' 。所以当你在 user_queryset 中得到 None 时,它​​的 print profile.html 模板
    • 我已经在编辑中添加了我的 js 代码。它似乎没有任何问题。它根据点击的内容记录正确的单词。有什么想法吗?
    • 您是否在模板中添加了 el 分页代码。我认为 el 分页和您的 js 一次调用相同的视图。当 el pagination 调用您的视图时,您在输出中得到 None 并且当您调用 js 时,您得到另一个输出,因为您正在发送 js。明白了吗?
    • 我在编辑中添加了我的模板,你能看看吗?是的,我有点理解你在说什么,但是在不使用 js 的情况下,我还能如何允许超过 1 个查询集(发布、评论等)?我试图复制这一点,但我认为我的观点错了:django-el-pagination.readthedocs.io/en/latest/…
    【解决方案2】:

    你能在附近签入你的javascript吗:

       user_queryset = 'user_comments';
    

    尝试将其更改为:

       var user_queryset = 'user_comments';
    

    我假设,如果您直接访问 cmets,变量 user_queryset 将是未定义的,并且不会作为 'user_cmets' 传递。

    【讨论】:

    • 是的,我试过了,但没有解决任何问题。我刚刚编辑了我的 JS,它显示我之前声明了 user_queryset 变量,所以它不是未定义的。 JS 发送了正确的数据,问题似乎出在视图中。
    猜你喜欢
    • 2018-06-02
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2019-01-03
    • 1970-01-01
    • 2012-05-17
    • 2019-02-21
    相关资源
    最近更新 更多