Search code examples
djangopython-2.7django-templatesdjango-commentsdjango-pagination

Django comments pagination isnt working


there is a trouble I'm new to django and there is an issue I can't understand,

there is a view:

def article(request, article_id = 1, comments_page_number = 1):
    all_comments = Comments.objects.filter(comments_article_id = article_id)
    paginator = Paginator(all_comments, 2)
    comment_form = CommentForm
    args = {}
    args.update(csrf(request))
    args['article'] = Article.objects.get(id = article_id)
    args['comments'] =  paginator.page(comments_page_number)
    args['form'] = comment_form
    args['username'] = auth.get_user(request).username
    return render_to_response('article.html', args)

there is a template article.html

{% extends 'main.html' %}

{% block article %}
<h4>{{article.article_date}}</h4>
<h2>{{article.article_title}}</h2>
<p> {{article.article_body}}</p>
<hr>

<div class="large-offset-1 large-8 columns">
<p>Комментарии: </p>
{% for comment in comments %}
    <p>{{comment.comments_text}}</p>
    <hr>
{% endfor %}
    {% if username %}
    <form action="/articles/addcomment/{{article.id}}/" method="POST" >
        {% csrf_token %}
        {{form }}
        <input type="submit" class="button" value="Add comment">
    </form>
    {% endif %}
</div>
    <div class="row">
        <div class="large-3 large-offset-5 columns">
            <ul class="pagination">
                {% if comments.has_previous %}
                    <li class="arrow"><a href="/articles/get/{{article.id}}/comments/{{ comments.previous_page_number }}">&laquo;</a></li>
                {% else %}
                    <li class="arrow unavailable"><a href="">&laquo;</a></li>
                {% endif %}
                {% for page in comments.paginator.page_range %}
                    {% if page == comments.number %}
                        <li class="current"><a href="/articles/get/{{article.id}}/comments/{{ page }}/">{{ page }}</a></li>
                    {% else %}
                        <li><a href="/articles/get/{{article.id}}/comments/{{ page }}/">{{ page }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if comments.has_next %}
                    <li class="arrow"><a href="/articles/get/{{article.id}}/comments/{{ comments.next_page_number }}/">&raquo;</a></li>
                {% else %}
                    <li class="arrow unavailable"><a href="">&raquo;</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

{% endblock %}

this is my article/urls.py

urlpatterns = patterns('',
    url(r'^articles/get/(?P<article_id>\d+)/$','article.views.article'),
    url(r'^articles/get/(?P<article_id>\d+)/comments/(\d+)/$', 'article.views.article'),
)

after that on my article page appeared an pages pagination, but when I'm clicking on the second page, for example, it it is just changing my url, but new comments are not appearing, just old ones.

What should I do to do this right? Thank you very much!


Solution

  • Your variable name comments_page_number uses always the default value. Name your second parameter in the url route to match this variable name.