完善博客
页面概要
博客主页面
博客文章内容页面
博客撰写页面
博客主页面
文章标题列表,超链接
发表博客按钮(超链接)
列表编写思路
取出数据库中的所有文章列表
将文章对象打包成列表,传递到前端
前端页面将文章以标题超链接的形式逐个列出
模板for循环
{% for xx in xxs %}
HTML 语句
{% endfor %}
获取数据库里所有内容
def index(request):
# article = models.Article.all()
# article = models.Article.objects.get(pk=1) # 获取单条数据
articles = models.Article.objects.all() # 获取所有数据,返回的为一个列表
return render(request, 'blog/index.html', {"articles": articles}) # 第三个参数跟一个字典类型的数据
更新index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
<a href="">新文章<a/>
<h1/>
{% for article in articles %}
<a href="">{{ article.title }}</a>
<br/>
{% endfor %}
</body>
</html>
添加一个跳转新文章的超链接
使用for循环,标题设置成超链接,链接对象待添加
访问页面如下: