【django】【my_blog】详情页面功能

详情页面功能

完成详情页逻辑

  • 修改:blog/view/article.py,添加details_page:
# 文章详情页
def details_page(request, article_id):
    # 读取文章详情
    article_details = models.article.Article.objects.get(pk=article_id)
    return render(request=request, template_name='article/details.html', context={'article_details': article_details})
  • 修改:blog/url.py,添加对应的URL:
url(regex=r'^article/details/(?P<article_id>[0-9]+)/$', view=article.details_page, name='article$details_page'),
  • 创建模板文件:blog/templates/article/details.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章详情页:{{ article_details.title }}</title>
</head>
<body>
<h2>{{ article_details.title }}</h2>
<h3>{{ article_details.content }}</h3>
</body>
</html>

检查结果,访问:http://localhost:8000/blog/article/details/1/

image

渲染链接

  • 修改文件:blog/templates/article/index.html
    在a标签中,修改herf属性
<li><a href="{% url 'blog:article$details_page' article.id %}">{{ article.title }}</a></li>

检查结果,访问:http://localhost:8000/blog/article/index/

image

推荐阅读更多精彩内容