У меня есть 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, который определяет, какой набор запросов используется. Поэтому, когда нажимают кнопку "больше комментариев" или "больше сообщений" (в шаблоне), чтобы получить больше объектов с разбивкой на страницы, я знаю, с какого запроса это нужно.
Однако, когда я использую приведенный выше код и нажимаю "больше" для разбиения на страницы ajax, он добавляет всю страницу, а не соответствующий дочерний шаблон (user_posts.html
или user_comments.html)
. Но блок кода if request.is_ajax()
работает нормально, он печатает правильный шаблон, чтобы этого не происходило.
Когда я меняю этот блок кода на этот
if request.is_ajax():
page_template = 'user_posts.html'
Работает анимация ajax для Post
. Однако я хотел бы добавить ajax pagination для Comment
. Почему мой начальный if request.is_ajax()
не работает и как я могу его исправить?
EDIT:
Вывод, когда я нажимаю на 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' '...' %}