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
Answers 1
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.
Answers 2
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'.
0 comments:
Post a Comment