Tôi có 2 queryset: Đăng và Nhận xét. Tôi đang sử dụng django-el-pagination để hiển thị chúng bằng ajax.Nhiều phân trang (ajax) không hoạt động đối với django-el-pagination
Dưới đây là quan điểm của tôi:
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)
Tôi có một cuộc gọi ajax đó tìm ra các truy vấn tập hợp đang được sử dụng. Vì vậy, khi 'nhiều nhận xét' hoặc 'nhiều bài đăng' (trong mẫu) đang được nhấp để có được nhiều đối tượng được phân trang hơn, tôi biết truy vấn nào từ đó. Tuy nhiên, khi tôi sử dụng mã ở trên và nhấp vào 'thêm' để phân trang ajax, nó sẽ thêm toàn bộ trang, không phải mẫu con phù hợp (user_posts.html
hoặc user_comments.html)
. Nhưng khối mã if request.is_ajax()
hoạt động tốt; này không nên xảy ra.
Khi tôi thay đổi điều đó khối mã này
if request.is_ajax():
page_template = 'user_posts.html'
Các ajax pagination cho Post
công trình. Tuy nhiên tôi muốn thêm pagination ajax cho Comment
là tốt. Tại sao không công việc ban đầu của tôi là if request.is_ajax()
và làm cách nào để khắc phục sự cố?
EDIT:
Sản lượng khi tôi bấm vào 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 (mẫu con)
{% 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' '...' %}
Có bạn nói đúng, chắc chắn là như vậy. Đối với một số lý do nó in hai lần, lần đầu tiên in mẫu gốc ('profile.html') là sai; nó chỉ nên in mẫu con ('user_posts.html') Bất kỳ ý tưởng tại sao nó sẽ làm điều này? Tôi đã thêm đầu ra trong bản chỉnh sửa của mình. – Zorgan
hai lần gọi điện có nghĩa là bạn đang làm điều gì sai trong js để gọi chế độ xem này. bạn đã định nghĩa page_ template = 'profile.html' trước phương thức is_ajax(). vì vậy khi bạn có Không trong user_queryset sao cho mẫu print.html của nó in –
Tôi đã thêm mã js của mình vào bản chỉnh sửa. Dường như không có gì sai với nó; nó ghi lại từ chính xác tùy thuộc vào những gì được nhấp. Bất kỳ ý tưởng? – Zorgan