导航的逻辑是判断用户是否登录,登录了显示登录看到的信息,没登录显示注册和登录。
渲染文章列表需要先注册admin,然后在admin里加入几篇文章,然后在视图函数里提取文章数据,并渲染到模板。需要注意的是显示用户头像的时候,需要在用户头像路径前加上media/
index.py和logout.py
# 首页def index(request): article_list = models.Article.objects.all() context = { 'article_list': article_list, } return render(request, 'index.html', context=context)# 注销def logout(request): auth.logout(request) # request.session.flush() return redirect(reverse('blog:login'))
index.html
index Panel heading without titlePanel contentPanel heading without titlePanel contentPanel heading without titlePanel content{% for article in article_list %}{ { article.title }}
{ { article.user.username }} 发布于{ { article.created_time | date:'Y-m-d H:i' }} 评论{ { article.comment_count }} 点赞{ { article.up_count }}
{% endfor %}Panel heading without titlePanel contentPanel heading without titlePanel content