(7)ジャンゴベースの個人的なブログサイトを構築

(7)ジャンゴベースの個人的なブログサイトを構築

序文

元に基づいていくつかの小さな機能を追加または変更

具体的な内容

1.コードの強調表示

オリジナルのブログ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>

これは自動的に、コード部分、すなわちに変換値下げによって強調され、


<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

そして、ミドルウェアの設定が追加します

次に、修正機能を表示する、本来の機能を容易にするために、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.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を得ることができた後、

おすすめ

転載: www.cnblogs.com/sfencs-hcy/p/10986169.html