0

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!

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
alohamaloha
  • 147
  • 1
  • 12
  • Are we in the Middle Ages or why do you create urls manually instead of using `{% url %}` template tag? .) – yedpodtrzitko Jun 03 '14 at 16:28
  • print your variable `comments_page_number` in the function to see it's not always `1`. If so, name your second parameter in the url route to match this variable name. – yedpodtrzitko Jun 03 '14 at 16:32
  • @yedpodtrzitko You are genius, i've added it in url, and it worked! Thank you! What did you say about template {% url %} tag...? – alohamaloha Jun 03 '14 at 17:22
  • ad `{% url %}` template tag: you can use it instead of creating urls manually in a template (see docs https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url ). This will help you eg. in case you'll change a structure of your URLs in `urls.py`, it won't break your links in templates. – yedpodtrzitko Jun 03 '14 at 17:31

2 Answers2

0

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

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
0

you need :

url(r'^articles/get/(?P<article_id>\d+)/comments/(?P<comments_page_number>\d+)/$', 'article.views.this_article'),
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37