I am making next and previous post button work on my blog in Django/Python. I thought it would be possible if I give a next or previous ID of articles to my URL like http://localhost:8000/3/ .
So I did as follows
next_article = Article.objects.filter(id__gt=article.id).order_by('id').first()
previous_article = Article.objects.filter(id__lt=article.id).order_by('id').first()
<li><a href=" {{ previous_article }} ">Previous</a></li>
<li><a href=" {{ next_article }} ">Next</a></li>
their return values are definitely just the numbers when I print them in my console. but it doesnt work on my real blog. Their values are just the same as their original ID of the present post. How do I make this right?
What do you mean by doing:
<li><a href=" {{ previous_article }} ">Previous</a></li>
<li><a href=" {{ next_article }} ">Next</a></li>
Should href
be a url?
Maybe use:
<li><a href=" {% url 'your-article-url-name' article.id-1 %} ">Previous</a></li>
<li><a href=" {% url 'your-article-url-name' article.id+1 %} ">Next</a></li>
if your urls are like:
url(r'^\d+/$', views.your-article-view, name='your-article-url-name')
Or if you have defined get_absloute_url
for your article in the modle,you can use:
<li><a href=" {{ previous_article.get_absloute_url }} ">Previous</a></li>
<li><a href=" {{ next_article.get_absloute_url }} ">Next</a></li>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.