From a9f8083f92b9311f74a8d31dedc92a258210d046 Mon Sep 17 00:00:00 2001 From: Jingle <599194993@qq.com> Date: Tue, 3 Mar 2020 19:00:45 +0800 Subject: [PATCH 1/2] add LICENSE. --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8bb1633 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Jingle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. -- Gitee From 7a00eda5b0cf6ce821ca94ffb1795d573161bc6c Mon Sep 17 00:00:00 2001 From: newpanjing Date: Thu, 12 Mar 2020 16:31:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8A=A0=E7=BC=93=E5=AD=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cms/models.py | 1 + mobile/views.py | 12 ++++++++++- requirements.txt | 3 ++- simple_cms/config.py | 4 ++++ simple_cms/settings.py | 11 ++++++++++ simple_cms/templatetags/cmstags.py | 13 ++++++++++++ simple_cms/utils.py | 34 ++++++++++++++++++++++++++++++ simple_cms/views.py | 16 ++++++++++++-- static/js/core.js | 11 ++++++++-- templates/article.html | 9 ++++++-- templates/base.html | 8 +++---- templates/category.html | 3 +-- templates/index.html | 18 +++++++--------- templates/mobile/article.html | 10 +++++++-- templates/mobile/category.html | 2 +- templates/mobile/index.html | 2 +- templates/search/search.html | 3 +-- templates/side.html | 2 +- templates/tag.html | 4 ++-- 19 files changed, 133 insertions(+), 33 deletions(-) create mode 100644 simple_cms/utils.py diff --git a/cms/models.py b/cms/models.py index faa0cf7..a87bd5a 100644 --- a/cms/models.py +++ b/cms/models.py @@ -4,6 +4,7 @@ import time from django.db import models # Create your models here. +from django.templatetags.static import static from django.utils.html import format_html from ueditor.fields import RichTextField diff --git a/mobile/views.py b/mobile/views.py index a3b91cf..ff42800 100644 --- a/mobile/views.py +++ b/mobile/views.py @@ -3,15 +3,22 @@ from django import shortcuts from cms.models import Page, Category, Article, Tag +from simple_cms import utils + +from django.views.decorators.cache import cache_page + def render(request, template_name, context=None, content_type=None, status=None, using=None): - return shortcuts.render(request, 'mobile/{}'.format(template_name), context, content_type, status, using) + # return shortcuts.render(request, 'mobile/{}'.format(template_name), context, content_type, status, using) + return utils.render(request, 'mobile/{}'.format(template_name), context, content_type, status, using) +@cache_page(60 * 10) def index(request): return render(request, 'index.html') +@cache_page(60 * 10) def category(request, alias, page=1): # 先找自定义页面,如果没有才是分类 @@ -30,6 +37,8 @@ def category(request, alias, page=1): }) +# 文章缓存24小时 +@cache_page(60 * 60 * 24) def aritlce(req, alias, id): a = Article.objects.get(id=id) a.hits += 1 @@ -41,6 +50,7 @@ def aritlce(req, alias, id): }) +@cache_page(60 * 10) def tag(request, name, page=1): tag = Tag.objects.filter(name=name).first() return render(request, 'tag.html', { diff --git a/requirements.txt b/requirements.txt index 8ee012c..0a2a8aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,5 @@ Markdown gunicorn django-hosts newspaper3k -django-import-export==1.2.0 \ No newline at end of file +django-import-export==1.2.0 +django-redis \ No newline at end of file diff --git a/simple_cms/config.py b/simple_cms/config.py index 14150fc..778fba0 100644 --- a/simple_cms/config.py +++ b/simple_cms/config.py @@ -10,3 +10,7 @@ USER = 'myblog' PASSWORD = '5rWEu98A' DEBUG = True + +# redis部分 + +REDIS_HOST = 'redis://127.0.0.1:6379/1' diff --git a/simple_cms/settings.py b/simple_cms/settings.py index 89a2b45..15cde27 100644 --- a/simple_cms/settings.py +++ b/simple_cms/settings.py @@ -196,3 +196,14 @@ APPEND_SLASH = True # hosts ROOT_HOSTCONF = 'simple_cms.hosts' DEFAULT_HOST = 'pc' + + +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": config.REDIS_HOST, + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + } + } +} \ No newline at end of file diff --git a/simple_cms/templatetags/cmstags.py b/simple_cms/templatetags/cmstags.py index 70f91ca..f6cac64 100644 --- a/simple_cms/templatetags/cmstags.py +++ b/simple_cms/templatetags/cmstags.py @@ -155,6 +155,7 @@ def get_tag(name, page=1, size=10): 'url': url } + @register.simple_tag def get_tag_random(size=5): count = Tag.objects.count() @@ -300,3 +301,15 @@ def get_ad(key, platform=0, style=''): }) return format_html(html) + + +@register.simple_tag(takes_context=True) +def get_current_host(context): + req = context.request + return "{}://{}".format(req.scheme, req.META.get("HTTP_HOST")) + + +@register.simple_tag(takes_context=True) +def get_current_url(context): + req = context.request + return get_current_host(context) + req.path diff --git a/simple_cms/utils.py b/simple_cms/utils.py new file mode 100644 index 0000000..faaae91 --- /dev/null +++ b/simple_cms/utils.py @@ -0,0 +1,34 @@ +from django.http import HttpResponse +from django.template import loader +import re + + +def compression(html): + """ + 移除多余的空格,换行 + :param html: + :return: + """ + html = re.sub("\r", "", html) + html = re.sub("\n|\t", " ", html) + html = re.sub("[ ]{2,}", " ", html) + + return html + + +def render(request, template_name, context=None, content_type=None, status=None, using=None): + """ + 移除多余的空格,换行符等 + :param request: + :param template_name: + :param context: + :param content_type: + :param status: + :param using: + :return: + """ + content = loader.render_to_string(template_name, context, request, using=using) + # 压缩 + content = compression(content) + + return HttpResponse(content, content_type, status) diff --git a/simple_cms/views.py b/simple_cms/views.py index 1c190d6..4970325 100644 --- a/simple_cms/views.py +++ b/simple_cms/views.py @@ -4,14 +4,20 @@ import json from django.template.loader import get_template from ueditor import site from django.http import HttpResponse -from django.shortcuts import render +# from django.shortcuts import render +from simple_cms.utils import render from django.views.decorators.csrf import csrf_exempt from system.models import SystemConfig from cms.models import * import random +from django.views.decorators.cache import cache_page +from django.utils.decorators import method_decorator + +# 单位是秒 +@cache_page(60 * 10) def index(request): # 每个分类显示4篇文章 category_newest = [] @@ -79,6 +85,8 @@ def settings_save(req): }), content_type="application/json") +# 文章缓存24小时 +@cache_page(60 * 60 * 24) def aritlce(req, category, id): a = Article.objects.get(id=id) a.hits += 1 @@ -89,6 +97,7 @@ def aritlce(req, category, id): }) +@cache_page(60 * 10) def category(req, category_alias=None, page=1): # 先找自定义页面,如果没有才是分类 @@ -115,16 +124,18 @@ def ueditor_upload(request): return site.handler(request) +@cache_page(60 * 2) def sitemap_xsl(request): engine = get_template('sitemap/sitemap.xsl') return HttpResponse(content=engine.render({}), content_type="text/html") # sitemap +@cache_page(60 * 2) def sitemap(request): # 文章只加载前1000篇防止爆内存 articles = Article.objects.filter(published=True).values('category__alias', 'id', 'update_date').all().order_by( - "-id")[:1000] + "-id")[:5000] domain = request.scheme + "://" + request.META.get("HTTP_HOST") categorys = Category.objects.all().values('alias').order_by("sort") pages = Page.objects.filter(display=True).values('alias', 'update_date') @@ -138,6 +149,7 @@ def sitemap(request): return HttpResponse(content=content, content_type="application/xml") +@cache_page(60 * 10) def tag(request, name, page=1): tag = Tag.objects.filter(name=name).first() return render(request, 'tag.html', { diff --git a/static/js/core.js b/static/js/core.js index 9325f4f..3b40f74 100644 --- a/static/js/core.js +++ b/static/js/core.js @@ -15,7 +15,7 @@ document.addEventListener('scroll', function (e) { if (t >= 35) { //navbar navbar.classList.add('navbar-fixed'); - }else{ + } else { navbar.classList.remove('navbar-fixed'); } @@ -23,6 +23,14 @@ document.addEventListener('scroll', function (e) { document.documentElement.scrollTop = 0 } }); +window.addEventListener('load', function () { + document.querySelectorAll("img").forEach(item => { + console.log(item.src) + if (!item.src || item.src == location.href) { + item.src = '/static/image/no_image.png'; + } + }); +}); function _addFavorite() { var url = window.location; @@ -66,4 +74,3 @@ function SetHome(obj, vrl) { } } } - diff --git a/templates/article.html b/templates/article.html index 98d05a2..8520eb9 100644 --- a/templates/article.html +++ b/templates/article.html @@ -16,8 +16,13 @@ {% endif %} - - + + + + + + + {% endblock %} {% block main %} {% block float-btn %}
- {#
#} - {# #} - {#
#}
{% endblock %} +{% block footer_js %} + +{% endblock %} {% include 'footer.html' %} diff --git a/templates/category.html b/templates/category.html index a894e65..9b51ef4 100644 --- a/templates/category.html +++ b/templates/category.html @@ -35,8 +35,7 @@
diff --git a/templates/index.html b/templates/index.html index b7dd24d..bd0fae1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -29,9 +29,7 @@
  • + target="_blank"> {% endfor %} @@ -51,7 +49,7 @@
    @@ -128,8 +126,8 @@
    @@ -176,8 +174,8 @@ {{ item.title }} + + alt="{{ item.title }}">
    diff --git a/templates/mobile/article.html b/templates/mobile/article.html index 945f975..2528536 100644 --- a/templates/mobile/article.html +++ b/templates/mobile/article.html @@ -11,8 +11,14 @@ {{ article.title }}- {{ settings.site_name }} - - + + + + + + + + {% endblock %} {% block navbar %}