当前位置: 移动技术网 > IT编程>脚本编程>Python > 基于django的个人博客网站建立(七)

基于django的个人博客网站建立(七)

2019年06月10日  | 移动技术网IT编程  | 我要评论

杀毒软件排行榜2015,科瑞计算簿注册码,父子摸螺蛳溺亡

基于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 uuid

class useridmiddleware(middlewaremixin):

    def process_request(self, request):
        try:
            uid = request.cookies['uid']
        except keyerror:
            uid = uuid.uuid4().hex
        request.uid = uid

    def 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:
            pass
        article_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 = false
        uid = self.request.uid
        read_id =uid+self.request.path+str(date.today())
        if not cache.get(read_id):
            increase = true
            cache.set(read_id,1,24*60*60)
        return increase

最后在页面一并将浏览数目显示即可

3.添加sitemap

在blog下建立sitemap.py

from django.contrib.sitemaps import sitemap
from django.urls import reverse

from backend import models

class articlesitemap(sitemap):
    changefreq = 'always'
    priority = 1.0
    protocol = 'http'

    def items(self):
        return models.article.objects.all()

    def lastmod(self,obj):
        return obj.creationtime

    def 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 articlesitemap

urlpatterns = [
    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 就可以得到

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网