django博客分享
这次向大家分享一下本人做的半成品博客,后续可以继续完善,因为还没想好博客需要展示什么,所以暂时直接分享出来吧。源码会放到github上面,有兴趣的可以下载来看一下吧。
博客结构
(1)在这里创建了blog、article、board和picture4个应用,就是使用:
python manage.py startapp 应用名
命令语句创建的。在这里主要为大家简单介绍下里面的功能。
(2)由于base.html模板是所有应用都继承的,所以放在根目录的templates下。
mysite下的urls.py文件
对于每个应用,使用include包括该应用下的路由,这样可以分别在每个应用app下创建一个urls,管理自己的路由。
blog应用
# blog.urls.py
from django.urls import re_path, path
from . import views
app_name = 'blog'
urlpatterns = [
re_path(r'blog/$', views.index, name='blog_index'),
]
(1)blog应用只需要管理好博客首页的路径,对于博客首页出现的东西,就交由给它。app_name是为了html中反向解析经常会用到app_name 但有时候又是namespace具体可以参考学习以下连接:
django中app_name以及namespace理解
# blog.views.py
from django.shortcuts import render
from article.views import AddArticle, AddArticle2
# Create your views here.
def index(request):
# url中直接访问时,返回首页
articles = AddArticle.objects.all()
articles2 = AddArticle2.objects.all()
context = {'articles': articles,
'articles2': articles2}
return render(request, "index.html", context)
(2)上面的就是blog的视图函数,只要是访问就读取数据库,实现从数据库中读取文章显示到页面中。
这里大家可以优化的,使用redis也好也可以使用Memcache进行缓存。缓存也比较简单,在setting中配置。然后设置对于文章从数据库中读取的数据保存到缓存中,设置生命周期为1d或者7d也可以,因为文章数量改变不大,自己写文章1天内不会有多少,所以可以进行优化,这样就不会每次一个访问都读取数据库。以下链接是介绍django的6种缓存,大家可以看一下:
Django中提供了6种缓存方式
(3)在这里简单说一下index.html中的这一句加载语句。由于在后台管理页面中,本人添加了富文本编辑,会记录html中的一些标签,在显示的时候需要过滤。这里使用了过滤器。
article应用
# article.urls.py
from django.urls import re_path, path
from . import views
app_name = 'article'
urlpatterns = [
re_path(r'add_article/$', views.AddArticleView.as_view(), name='add_article'),
re_path(r'article_detail/(\d+)/$', views.article_detail, name='article_detail'),
re_path(r'article2_detail/(\d+)/$', views.article2_detail, name='article2_detail'),
re_path(r'post_list/$', views.post_list, name='post_list'),
]
(1)这里其实只有3个路径,添加文章,文章详情和显示文章列表。前期为了测试简单的文章详情显示的时候,写了一个。可能当时是想保留2个吧,现在想想有点多余了。
# article.views.py
import time
from django.shortcuts import render, redirect, reverse
from django.views.generic import View
from .models import AddArticle, AddArticle2
# Create your views here.
class AddArticleView(View):
def get(self, request):
return render(request, "add_article.html")
def post(self, request):
data = AddArticle()
data.title = request.POST.get("title")
data.author = "测试作者"
data.part = "测试类"
data.public_time = time.strftime('%Y-%m-%d %H:%M:%S.000000', time.localtime(time.time()))
data.read_num = "100"
data.comment_num = "12"
data.body = request.POST.get("body")
data.save()
return redirect(reverse('index'))
def article_detail(request, id):
# 取出相应的文章
article = AddArticle.objects.get(id=id)
# 需要传递给模板的对象
context = {'article': article}
# print(context)
# 载入模板,并返回context对象
return render(request, 'article_detail.html', context)
def article2_detail(request, id):
article = AddArticle2.objects.get(id=id)
context = {'article': article}
# print(article.body)
# 载入模板,并返回context对象
return render(request, 'article_detail.html', context)
def post_list(request):
article = AddArticle2.objects.all().order_by('-public_time')
context = {'articles': article}
return render(request, 'post_list.html', context)
(2)article视图函数就是实现文章添加和显示的功能逻辑的。这里添加文章的时候,阅读量read_num和评论数comment_num可以初始化为0的,也可以用分models进行管理,然后保存在缓存中,定周期的更新到数据库中保存。如果是比较多流量阅读的,可以这么优化,基本上个人自己的博客没几个人会看的,也就不需要做这么多了。
# article.modules.py
from django.db import models
from tinymce.models import HTMLField
# Create your models here.
class AddArticle(models.Model):
title = models.CharField(max_length=120)
author = models.CharField(max_length=120)
public_time = models.DateTimeField()
part = models.CharField(max_length=200)
read_num = models.IntegerField()
comment_num = models.IntegerField()
body = models.TextField()
class AddArticle2(models.Model):
title = models.CharField(max_length=120)
author = models.CharField(max_length=120)
public_time = models.DateTimeField()
part = models.CharField(max_length=200)
read_num = models.IntegerField()
comment_num = models.IntegerField()
body = HTMLField()
(3)这个modules模板是用后台来进行添加的,因为在后台添加了一个富文本编辑器tinymce,将文章主体保存为html文件。同时,该models中要添加以下语句:
from tinymce.models import HTMLField
其他的应用app差不多同样的原理吧,大家可以去观看源码的。
在这里多说一下这个地方。点击对应的文字链接到的地址是url下atricle的app_name下的namespace。例如在board的url就是在board应用的urls.py文件下app_name就是第一个参数,后面跟着的board_message就是url地址后面的name参数。
github地址
本人github地址
大家也可以关注一下我的公众号,有什么不懂的问题可以后台留言呀,看到后如果本人会的话就会回复你的。大家一起共勉!!