基于django的个人博客网站建立(七)
前言
网站效果可点击这里访问
这次在原来的基础上添加或修改一些小功能
具体内容
1.代码高亮
在原来的blog-details.html页面添加下面的代码:
<link href="http://cdn.bootcss.com/highlight.js/9.12.0/styles/googlecode.min.css" rel="stylesheet"><script src="http://cdn.bootcss.com/highlight.js/8.0/highlight.min.js"></script><script>hljs.initHighlightingOnLoad();</script>
它会自动高亮由markdown转换成的代码部分,即
<pre><code></code></pre>
2.统计文章阅读数量
通过在用户浏览器上存储唯一id来保证识别用户
每篇文章每个浏览器只能够每天一次增加浏览数目
首先先为article表添加浏览数目字段
class Article(models.Model):title = models.CharField(max_length=128)markdownContent = models.TextField(default='')htmlContent = models.TextField()read_num = models.IntegerField(default=0)creationTime = models.DateTimeField(auto_now_add=True)
然后通过中间件的方式来为用户浏览器设置唯一id
from django.utils.deprecation import MiddlewareMixin
import uuidclass UserIdMiddleware(MiddlewareMixin):def process_request(self, request):try:uid = request.COOKIES['uid']except KeyError:uid = uuid.uuid4().hexrequest.uid = uiddef process_response(self, request, response):response.set_cookie('uid',request.uid,max_age=60*60*24*365*10,httponly=True)return response
并在setting中把中间件加入
接下来修改视图函数,为了方便将原来的视图函数改成了CBV
class Blog_details(View):def get(self,request,*args,**kwargs):all_type = models.ArticleType.objects.all()article_id = request.GET.get('article_id')if self.is_increase():models.Article.objects.filter(id=article_id).update(read_num=F('read_num') + 1)else:passarticle_obj = models.Article.objects.filter(id=article_id).first()return render(request, 'show/blog-details.html', {'article_obj': article_obj, 'all_type': all_type})def is_increase(self):increase = Falseuid = self.request.uidread_id =uid+self.request.path+str(date.today())if not cache.get(read_id):increase = Truecache.set(read_id,1,24*60*60)return increase
最后在页面一并将浏览数目显示即可
3.添加sitemap
在blog下建立sitemap.py
from django.contrib.sitemaps import Sitemap
from django.urls import reversefrom backend import modelsclass ArticleSitemap(Sitemap):changefreq = 'always'priority = 1.0protocol = 'http'def items(self):return models.Article.objects.all()def lastmod(self,obj):return obj.creationTimedef location(self,obj):return 'blog-details/?article_id='+str(obj.id)
在temlpates下编写sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">{% spaceless %}
{% for url in urlset %}
<url>
<loc>
{{ url.location }}
</loc>
<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>
<changefreq>{{ url.changefreq }}</changefreq><priority>{{ url.priority }}</priority></url>{% endfor %}{% endspaceless %}
</urlset>
添加url
from django.contrib.sitemaps import views as sitemap_views
from blog.sitemap import ArticleSitemapurlpatterns = [path('admin/', admin.site.urls),path('',views.index),path('index/',views.index),path('backend/',include('backend.urls')),path('blog-details/',views.Blog_details.as_view(),name="blog-details"),path('saysomethingtome/', views.saysomethingtome),path('article_comment/',views.article_comment),path('category/',views.category),path('category/details/', views.category_details),path('record/', views.record),path('about/', views.about),path('sitemap.xml/',sitemap_views.sitemap,{'sitemaps':{'article':ArticleSitemap}})
]
之后访问127.0.0.1:8000/sitemap.xml 就可以得到