def next_article(self):
# 下一篇
return Article.objects.filter(id__gt=self.id).order_by('id').first()
def pre_article(self):
# 上一篇
return Article.objects.filter(id__lt=self.id).order_by('-id').first()
a = 2
arr = [2, 3, 1]
num = arr.index(a)
a = 2
arr = [1, 3, 2]
a = 2
arr = [1, 2, 3]
a = 2
arr = [2]
if num == 0 and num != len(arr) - 1:
print('next')
elif num != 0 and num == len(arr) - 1:
print('pre')
elif num != 0 and num != len(arr) - 1:
print('next')
print('pre')
elif num == 0 and num == len(arr) - 1:
pass
if num == 0:
if num == len(arr) - 1:
pass
else:
print('next')
else:
if num == len(arr) - 1:
print('pre')
else:
print('pre')
print('next')
def blogdetail_view(request, id):
blogDetail = blog.objects.get(id=id)
return render(request, 'blog/blogDetail.html', locals())
blogs = blog.objects.filter(list=blogDetail.list).order_by('-index', '-created', '-id')
# 这个values_list不加在上边blogs=的后面是因为我的右侧导航栏是需要blogs而不是blogsList
blogsList = list(blogs.values_list('id', flat=True))
blogsListNum = blogsList.index(id)
if blogsListNum == 0:
if blogsListNum == len(blogsList) - 1:
pass
else:
nextBlog = blog.objects.get(id=blogsList[blogsListNum + 1])
else:
if blogsListNum == len(blogsList) - 1:
preBlog = blog.objects.get(id=blogsList[blogsListNum - 1])
else:
preBlog = blog.objects.get(id=blogsList[blogsListNum - 1])
nextBlog = blog.objects.get(id=blogsList[blogsListNum + 1])
def blogdetail_view(request, id):
blogDetail = blog.objects.get(id=id)
// exclude(tag=2)表示不搜索隐藏的博客
blogs = blog.objects.filter(list=blogDetail.list).order_by('-index', '-created', '-id').exclude(tag=2)
blogsList = list(blogs.values_list('id', flat=True))
blogsListNum = blogsList.index(id)
if blogsListNum == 0:
if blogsListNum == len(blogsList) - 1:
pass
else:
nextBlog = blog.objects.get(id=blogsList[blogsListNum + 1])
else:
if blogsListNum == len(blogsList) - 1:
preBlog = blog.objects.get(id=blogsList[blogsListNum - 1])
else:
preBlog = blog.objects.get(id=blogsList[blogsListNum - 1])
nextBlog = blog.objects.get(id=blogsList[blogsListNum + 1])
return render(request, 'blog/blogDetail.html', locals())
{% if preBlog or nextBlog %}
<div class="ta-blogDetail-controlBlog mt-4">
{% if preBlog %}
<a class="ta-blogDetail-preBlog" href="{% url 'blogdetail' preBlog.id %}">
<p>上一篇:{{ preBlog.title }}</p>
</a>
{% else %}
<a></a>
{% endif %}
{% if nextBlog %}
<a class="ta-blogDetail-nextBlog" href="{% url 'blogdetail' nextBlog.id %}">
<p>下一篇:{{ nextBlog.title }}</p>
</a>
{% else %}
<a></a>
{% endif %}
</div>
{% endif %}
.ta-blogDetail-controlBlog {
display: flex;
align-items: center;
justify-content: space-between;
}
/* 公共文件 */
--ta-dark-white: #ffffff;
--ta-dark-text: #bdbdbd;
.ta-blogDetail-preBlog, .ta-blogDetail-nextBlog{
color: var(--ta-dark-text);
}
.ta-blogDetail-preBlog:hover,
.ta-blogDetail-nextBlog:hover {
color: var(--ta-dark-white);
}
@media (max-width: 768px) {
.ta-blogDetail-controlBlog {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
flex-direction: column;
}
}
此页面不支持夜间模式!
已进入夜间模式!
已进入普通模式!
搜索框不允许为空
签到成功!经验+5!芋圆币+2!
签到失败!今日已签到!
需要登录社区账号才可以进入!