Django commente la pagination ne fonctionne pas
-
21-12-2019 - |
Question
Il y a un problème que je suis nouveau à Django et il y a un problème que je ne peux pas comprendre,
Il y a une vue:
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)
Il y a un 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 }}">«</a></li>
{% else %}
<li class="arrow unavailable"><a href="">«</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 }}/">»</a></li>
{% else %}
<li class="arrow unavailable"><a href="">»</a></li>
{% endif %}
</ul>
</div>
</div>
{% endblock %}
Ceci est mon 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'),
)
Après cela sur ma page d'article figurait une pagination de pages, mais lorsque je clique sur la deuxième page, par exemple, cela change simplement mon URL, mais de nouveaux commentaires ne figurent pas, les plus anciens.
Que dois-je faire pour faire ce droit?Merci beaucoup!
La solution
Votre nom de variable comments_page_number
utilise toujours la valeur par défaut.Nommez votre deuxième paramètre dans la route URL pour correspondre à ce nom de cette variable.
Autres conseils
Vous avez besoin de:
url(r'^articles/get/(?P<article_id>\d+)/comments/(?P<comments_page_number>\d+)/$', 'article.views.this_article'),
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow