diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..8bb16339c9deeabd6e86b0208fc38988614a4d7f --- /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. diff --git a/cms/models.py b/cms/models.py index faa0cf7dfd874a2f8cd974aa0c8f501fab24140e..a87bd5a02160926114f3f3c7a7cc04b88fdc44bc 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 a3b91cf0a797a56fb41770deaed379b4f953163f..ff4280039f766ae148807ee24f5b78f043374305 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 8ee012cec33093b518464dfb682d24c8f6a0ff2f..0a2a8aaa398381d40db3606c763a87c10b972cae 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 14150fc59167e3a76212c1caf860510575b4e93f..778fba083bf4c147e70e07a9ac18ab690e0c6533 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 89a2b4554609215eb7b389bb553346ac013e90ad..15cde27461f47ba9ab71068c258242a7af437d4d 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 70f91ca601b5c16e36b535ddd6e6c849a3e7e484..f6cac6450c7cc655b6147cce6016fc3bc676e7e9 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 0000000000000000000000000000000000000000..faaae91701ed592f5f53c1e0a966986c97fd1561 --- /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 1c190d6345b622f7b5b6147c389ceb04ddd69dea..4970325d438a715cb28bd174a9573a22a736ab86 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 9325f4f8597ee0eb237b8b20d22713cfd01631ac..3b40f7467dacdadd987fdf6616483e53edaa8a7b 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 98d05a241d4e249b4f79b6def25bf9e0ad75fffe..8520eb99b64c36a7501899ae983b466adacb9197 100644 --- a/templates/article.html +++ b/templates/article.html @@ -16,8 +16,13 @@ {% endif %} - - + + + + + + + {% endblock %} {% block main %}
{% block float-btn %}