当前位置:   article > 正文

用c语言对相遇问题编程,基于django的个人博客网站建立(七)

c语言相遇问题

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

前言

网站效果可点击这里访问

这次在原来的基础上添加或修改一些小功能

具体内容

1.代码高亮

在原来的blog-details.html页面添加下面的代码:

它会自动高亮由markdown转换成的代码部分,即

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

{% spaceless %}

{% for url in urlset %}

{{ url.location }}

{{ url.lastmod|date:"Y-m-d" }}

{{ url.changefreq }}

{{ url.priority }}

{% endfor %}

{% endspaceless %}

添加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 就可以得到

f860b13ebe4b2077404b5b3b33caa9a1.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/799804
推荐阅读
相关标签
  

闽ICP备14008679号