博客主页面开发
Django模板扩展(模板for循环)
{% for xs in xxs %}
HTML语句
{% endfor %}
更多模板拓展内容可看 http://www.ziqiangxuetang.com/django/django-template2.html
将APP下的views.py更改 选取后台models Article类下的数据列表集合
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def index(request):
arts = models.Article.objects.all()
return render(request,'blog/index.html',{'articles': arts})
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1><a href="#">我的博客</a> </h1>
{% for wz in articles %}
<a href="#">{{ wz.title }}</a>
<br/>
{% endfor %}
</body>
</html>
启动服务器 页面显示
我的博客
title1
title2