博客页面分类:
1.博客主页面
2.博客文章内容页面
3.博客撰写页面
主页面内容:
- 文章标题列表,超链接
- 取出数据库中所有文章对象
- 将文章对象打包成列表,传递到前端
- 前端页面把文章以标题超链接的形式逐个列出
后端 views.py
1
2
3
4
5
6
7
8
9# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from . import models
# 获取所有文章
def index(request):
articles = models.Article.objects.all() # 返回django内置的查询结果的集合对象,类似于列表
return render(request,'blog/index.html',{'articles':articles})前端 index.html
1
2
3
4
5
6
7
8
9
10
11
12<!--撰写博客入口-->
<h2>
<a href="">编写新文章</a>
</h2>
<!--文章列表-->
<h3>
文章列表:<br>
{% for article in articles %}
<a href="">{{ article.title }}</a>
<br/>
{% endfor %}
发表博客按钮(超链接)博客文章内容页面
- 利用文章 ID 获取单个文章对象
- 后端 view.py
1
2
3
4
5
6
7
8
9
10
11
12
13# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def index(request):
articles = models.Article.objects.all() # 返回django内置的查询结果的集合对象,类似于列表
return render(request,'blog/index.html',{'articles':articles})
# 获取文章对象
def article_page(request,article_id):
article = models.Article.objects.get(pk=article_id)
return render(request,'blog/article_page.html',{'article':article})
- 编写显示文章内容页面的前端
- 前端 articl_page.html
1
2
3
4
5
6<!--显示单个文章标题和内容-->
<h1>{{article.title}}</h1><br>
<h3>{{article.content}}</h3>
<br><br>
<!--修改文章链接-->
<a href="">修改文章</a>
- 修改 URL 配置
- urls.py
1
2
3
4
5
6
7
8# -*- coding:utf-8 -*-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index/$', views.index),
url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),
]