Establish (seven) django-based personal blog site

Establish (seven) django-based personal blog site

Foreword

The add or modify some small functions based on the original

specific contents

1. code highlighting

In the original blog-details.html page add the following code:


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

It will automatically highlighted by the markdown converted into the code portion, i.e.,


<pre><code></code></pre>

2. The number of articles read statistics

By storing a unique id on the user's browser to identify the user to ensure that

Each article once every browser can only increase the number of daily browsing

First, add the first number field to view the article table


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)

And then to set the unique id to the user's browser by way of middleware


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

And setting in the middleware added

Next, modifications view function, in order to facilitate the original function changed view 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

Finally, you can display the page in a browser and number

3. Add sitemap

In the blog establish 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)

Sitemap.xml written in temlpates


<?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>

Add 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}})
]

After you can get access 127.0.0.1:8000/sitemap.xml

Guess you like

Origin www.cnblogs.com/sfencs-hcy/p/10986169.html