前言
之前的博客首页,我们是直接拿的静态页面模板,里面都是写死的数据
那本章就实现 【博客首页动态获取】
ps:那这part实现起来很简单,我们只需要数据库数据返回,且把index.html的值取对应的属性即可
环境:
- Pycharm
- python3.6
- mysql 5.7
- django 2.0.13
一、新功能项目概览
- 大家要看效果的话,记得提前在后台添加点自己的数据
二、具体实现
1、user应用下views.py
- 把原index函数,加上数据库查询的数据即可
def index(request):
"""
返回首页
:param request:
:return:
"""
# 拿到前三条
figure_articles = Article.objects.all()[:3]
# 时间轴文章:我们拿到所有文章
darticles = Article.objects.all()
return render(request, "index.html", context={
'figure_articles': figure_articles, 'darticles': darticles})