25

I have 2 querysets: Post and Comment. I'm using django-el-pagination to render these using ajax.

Here's my view:

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)

I have an ajax call that find out which query set is being used. So when 'more comments' or 'more posts' (in the template) is being clicked to get more paginated objects, I know which queryset it's from. However when I use the above code and click 'more' for the ajax pagination, it appends the whole page, not the relevant child template (user_posts.html or user_comments.html). But the if request.is_ajax() code block works fine; it prints the correct template to use so this shouldn't be happening.

When I change that code block to this

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

The ajax pagination for Post works. However I'd like to add ajax pagination for Comment aswell. Why doesn't my initial if request.is_ajax() work and how can I fix it?

EDIT:

Output of when I click on on 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 (child template)

{% 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' '...' %}

2 Answers 2

9
+100

What is the output of below line?

print('Queryset:', user_queryset)

I think you have problem in below line

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

this is not returning correct get parameter value for match with condition of post and comment part.

7
  • Yes you're right, it's definately that. For some reason it prints twice, the first time printing the parent template (profile.html) which is wrong; it should only print the child template (user_posts.html) Any idea why it would do this? I've added the output in my edit.
    – Zorgan
    Jul 24, 2017 at 9:27
  • twice time calling means you are doing something wrong in js for call this view. you defined page_ template ='profile.html' before is_ajax() method . so when you got None in user_queryset so that its print profile.html template Jul 24, 2017 at 9:32
  • I've added my js code in the edit. There doesn't appear to be anything wrong with it; it logs the correct word depending on what's clicked. Any idea?
    – Zorgan
    Jul 24, 2017 at 9:48
  • did you add el pagination code in template. I think el pagination and your js calling same view at a time. when el pagination calling your view so you got None in output and when you js calling you view you got another ouput because you are sending in js. got it? Jul 24, 2017 at 9:57
  • I've added my templates in edit, could you have a look?. And yes I kind of understand what you're saying, but how else would I allow more than 1 queryset (post, comment, etc) without using js? I tried to replicate this but I think I did my views wrong: django-el-pagination.readthedocs.io/en/latest/…
    – Zorgan
    Jul 24, 2017 at 10:13
1

Could you check in your javascript near :

   user_queryset = 'user_comments';

Try to change it to :

   var user_queryset = 'user_comments';

I assume, if you go directly to comments, that the variable user_queryset will be undefined and it doesn't get passed as 'user_comments'.

1
  • Yeah I've tried that and doesn't fix anyhing. I just edited my JS which shows I declare the user_queryset variable before so it's not undefined. The JS sends the correct data, problem seems to be in the views.
    – Zorgan
    Sep 8, 2017 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.