视图层(views.py)
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from django.views.generic import View
class UserCommentsListView(View):
"""
用户评论列表详情
"""
def get(self, request, user_id):
user = get_object_or_404(UserProfile, id=int(user_id))
comments = user.user_comments.all().filter(parent=None)
paginator = Paginator(comments, 10)
page = request.GET.get('page')
try:
user_comments = paginator.page(page)
except PageNotAnInteger:
user_comments = paginator.page(1)
except EmptyPage:
if request.is_ajax():
return HttpResponse('')
user_comments = paginator.page(paginator.num_pages)
if request.is_ajax():
return render(request, 'public-comment-list-ajax.html', {'articles': user_comments})
main_top_info = '评论列表'
article_count = len(comments)
return render(request, 'public-comment-list.html', {
'comments': user_comments,
'comment_count': article_count,
'main_top_info': main_top_info
})
路由(urls.py)
from .views import UserCommentsListView
path('<int:user_id>/comments/', UserCommentsListView.as_view(), name='user_comments'),
模板(template)
// public-comment-list.html
{% extends "base.html" %}
{% block title %}
{{ main_top_info }}
{% endblock %}
{% block main_content %}
<div class="container index">
<div class="row">
<div class="col-xs-12 main">
<div class="main-top">
<h4>{{ main_top_info }}({{ comment_count }})</h4>
</div>
<hr>
<div id="list-container">
<ul class="note-list public-comment-list">
{% include 'public-comment-list-ajax.html' %}
</ul>
</div>
</div>
</div>
</div>
{% endblock %}
{% block custom_js %}
<script type="text/javascript">
$.ajaxSetup({
data: { csrfmiddlewaretoken: '{{ csrf_token }}' },
});
$(document).ready(function () {
var page = 1;
var empty_page = false;
var block_request = false;
$(window).scroll(function() {
var margin = $(document).height() - $(window).height() - 200;
if ($(window).scrollTop() > margin && empty_page == false && block_request == false) {
{% if comment_count < 10 %}
return false;
{% endif %}
block_request = true;
page += 1;
$.get('?page=' + page, function(data) {
if(data == '')
{
empty_page = true;
}
else {
block_request = false;
$('.public-comment-list').append(data);
}
});
}
});
});
</script>
{% endblock %}
// public-comment-list-ajax.html
{% for comment in comments %}
<li id="note-3" data-note-id="3">
<div class="content">
<div class="author">
{# <a class="avatar" href="{{ user_default.get_absolute_url }}"> <img src="{{ MEDIA_URL }}{{ user_default.avatar }}" alt="avatar"> </a>#}
<cite>
<span> 来自 </span>
<div class="name">
<a href="{{ comment.user.get_absolute_url }}">
{{ comment.user.get_username_or_nickname }}
</a>
<span>{{ comment.created }}</span>
</div>
</cite>
</div>
<p class="comment">{{ comment.content }}</p>
<blockquote>
<a class="title" href="{{ comment.article.get_absolute_url }}"> {{ comment.article.title }} </a>
<p class="abstract">{{ comment.article.summary }}...</p>
<div class="meta">
<a target="_blank" href="{{ comment.article.author.get_absolute_url }}" class="text-teal">
{{ comment.article.author.get_username_or_nickname }}
</a>
<a target="_blank" href="{{ comment.article.get_absolute_url }}"> <i class="fa fa-eye"></i> {{ comment.article.view_count }} </a>
<a target="_blank" href="{{ comment.article.get_absolute_url }}#comments"> <i class="fa fa-comment-o"></i> {{ comment.article.comments.count }} </a>
<span><i class=" fa fa-heart-o"></i> {{ comment.article.total_like }}</span>
</div>
</blockquote>
</div>
</li>
{% endfor %}