浏览代码

评论提交

yangchaojie 3 周之前
父节点
当前提交
591f7d8b28

+ 4 - 1
.idea/misc.xml

@@ -1,4 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Pipenv (CyberSicko)" project-jdk-type="Python SDK" />
+  <component name="Black">
+    <option name="sdkName" value="blog" />
+  </component>
+  <component name="ProjectRootManager" version="2" project-jdk-name="blog" project-jdk-type="Python SDK" />
 </project>

+ 6 - 4
blog/controller/article.py

@@ -57,8 +57,8 @@ def new(request):
     return render(request, 'management/article/new_article.html')
 
 
-def show_article(request, pk):
-    article = models.Article.objects.filter(id=int(pk)).values('id', 'title', 'intro', 'category',
+def show_article(request, title):
+    article = models.Article.objects.filter(title=title).values('id', 'title', 'intro', 'category',
                                                                'user', 'created_time', 'type',
                                                                'cover__file_net_path',
                                                                'music__file_net_path', 'status',
@@ -66,11 +66,13 @@ def show_article(request, pk):
                                                                'user__first_name', 'user_id',
                                                                'user__last_name', 'markdown_text', 'is_top',
                                                                'html_text').first()
-    comments = models.Comment.objects.filter(article_id=int(pk))
-
+    comments = models.Comment.objects.filter(article_id=article['id'])
+    # request.session['msg'] = ''
     from blog.views import get_record_and_tags
     result = get_record_and_tags()
 
+
+
     return render(request, 'blog.html', context={'article': article, 'records': result['records'],
                                                  'tags': result['tags'], 'comments': comments})
 

+ 25 - 4
blog/controller/comment.py

@@ -1,4 +1,6 @@
 import datetime
+import math
+import random
 
 from django.http import JsonResponse, HttpResponseRedirect
 
@@ -8,14 +10,33 @@ from system.error.ServerException import ServerException
 
 
 
+
+
+def get_very_code(request):
+    very_num = random.randint(1, 100)
+    request.session['very_num'] = very_num
+    return JsonResponse({"success": True, "data": very_num})
+
+
 def add_comment(request):
     if request.method == 'POST':
         nick_name = request.POST.get("nick_name")
         avatar = request.POST.get("avatar")
         website = request.POST.get("website")
         article_id = request.POST.get("article_id")
+        article_title = request.POST.get("article_title")
         comment = request.POST.get('comment')
         email = request.POST.get('email')
+        veryCode = request.POST.get('veryCode')
+        try:
+            veryCode = int(veryCode)
+            if request.session['very_num'] != veryCode:
+                request.session['msg'] = "验证码不正确"
+                return HttpResponseRedirect('/article/' + article_title + '.html')
+        except Exception as e:
+            request.session['msg'] = "验证码不正确"
+            return HttpResponseRedirect('/article/' + article_title + '.html')
+
         article = models.Article.objects.get(id=int(article_id))
         if article is None:
             return JsonResponse({"success": False, "message": "没有找到对应文章"}, safe=False)
@@ -25,22 +46,22 @@ def add_comment(request):
 
         if is_empty(nick_name):
             request.session['msg'] = "请填写昵称"
-            return HttpResponseRedirect('/article/' + article_id + '.html')  # 跳转到主界面
+            return HttpResponseRedirect('/article/' + article_title + '.html')  # 跳转到主界面
 
         if is_empty(comment):
             request.session['msg'] = "请填写评论内容"
-            return HttpResponseRedirect('/article/' + article_id + '.html')  # 跳转到主界面
+            return HttpResponseRedirect('/article/' + article_title + '.html')  # 跳转到主界面
 
         if is_empty(email):
             request.session['msg'] = "请填写Email"
-            return HttpResponseRedirect('/article/' + article_id + '.html')  # 跳转到主界面
+            return HttpResponseRedirect('/article/' + article_title + '.html')  # 跳转到主界面
 
         request.session['msg'] = ""
         models.Comment.objects.create(nick_name=nick_name, avatar=avatar, article=article,
                                                     website=website,
                                                     comment=comment,
                                                     created_time=datetime.datetime.now(), email=email)
-        return HttpResponseRedirect('/article/' + article_id + '.html')  # 跳转到主界面
+        return HttpResponseRedirect('/article/' + article_title + '.html')  # 跳转到主界面
 
     else:
         raise ServerException("错误的请求")

+ 1 - 1
blog/router/article_url/article_url.py

@@ -26,7 +26,7 @@ urlpatterns = [
     path('to_edit/<pk>', article.to_edit),
     path('cancel_show_index/<pk>', article.cancel_show_index),
     path('show_show_index/<pk>', article.show_show_index),
-    path(r'<pk>.html', article.show_article),
+    path(r'<title>.html', article.show_article),
     path('edit_article', article.edit_article),
     path('delete_article', article.delete_article),
 ]

+ 1 - 1
blog/router/comment_url/comment_url.py

@@ -21,7 +21,7 @@ from blog.controller import comment
 
 urlpatterns = [
     path('add', comment.add_comment),
-
+    path('getVeryCode', comment.get_very_code),
 
 ]
 urlpatterns += staticfiles_urlpatterns()

+ 96 - 2
templates/_common/base.html

@@ -39,6 +39,98 @@
         <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
 
     {% endif %}
+    <style>
+        :root {
+            --primary: #000000;
+            --bg-light: #f8f8f8;
+            --text: #333;
+            --gray: #666;
+            --line-height: 1.8;
+            --max-width: 900px;
+            --sidebar-width: 300px;
+        }
+
+        body {
+            color: var(--text);
+            background-color: #fff;
+            line-height: var(--line-height);
+        }
+
+
+
+        /* 主容器:限制最大宽度并居中 */
+        .ui.grid.stackable.container {
+            max-width: var(--max-width);
+            margin: 2rem auto;
+            padding: 0 1rem;
+        }
+
+        /* 段落与标题留白 */
+        p {
+            margin-bottom: 1rem;
+        }
+
+        h2.ui.header {
+            margin-top: 2rem;
+            margin-bottom: 1rem;
+        }
+
+        /* 卡片式内容区 */
+        .eleven.wide.column {
+            background: #fff;
+            padding: 2rem;
+            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
+            border-radius: 4px;
+        }
+
+        /* 代码块滚动与样式 */
+        .pre {
+            overflow-x: auto;
+            padding: 1rem;
+            background: #fafafa;
+            border-left: 3px solid var(--primary);
+            margin: 1.5rem 0;
+        }
+
+        /* 顶部菜单去掉死板的灰色,增加一点阴影 */
+        .ui.top.fixed.menu {
+            background: #fff;
+            box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
+        }
+
+        /* 菜单项内边距更舒适 */
+        .ui.menu .item {
+            padding: 0.8rem 1.2rem;
+            font-size: 1rem;
+        }
+
+        /* 移动端:展开动画更顺滑 */
+        .ui.vertical.menu {
+            transition: max-height .3s ease-out;
+            overflow: hidden;
+        }
+
+        .ui.vertical.menu.collapsed {
+            max-height: 0 !important;
+        }
+
+        .ui.vertical.menu.expanded {
+            max-height: 500px !important;
+        }
+
+        /* 右侧 Archiv/Top 区域在大屏下保持粘性 */
+        .four.wide.right.floated.column {
+            position: sticky;
+            top: 80px; /* 根据你的导航高度微调 */
+            max-width: var(--sidebar-width);
+            margin-left: 2rem;
+        }
+
+        /* 调整归档和标签列表间距 */
+        .ui.list .item {
+            padding: .5rem 0;
+        }
+    </style>
     <style type="text/css">
         p {
             font-size: 1.2em;
@@ -114,6 +206,8 @@
         {% block css %}
 
         {% endblock %}
+
+
     </style>
 
 </head>
@@ -125,7 +219,7 @@
 <div class="ui tablet computer only padded grid">
     <div class="ui top fixed borderless fluid huge menu">
         <div class="ui container">
-            <a href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
+            <a href="/" class="header item " >CyberSicko.net</a>
             <a href="{% url 'index' %}" class="{% if current_category == None %}
                 active
                 {% else %}
@@ -155,7 +249,7 @@
 </div>
 <div class="ui mobile only padded grid">
     <div class="ui top fixed borderless huge fluid menu">
-        <a href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
+            <a href="/" class="header item " >CyberSicko.net</a>
         <div class="right menu">
             <div class="item">
                 <button class="ui icon toggle basic button">

+ 155 - 9
templates/blog.html

@@ -38,6 +38,104 @@
     <link rel="stylesheet" href="{% static 'highlightjs/styles/androidstudio.min.css' %}">
     <link rel="stylesheet" href="{% static 'css/fonts.css' %}">
     <link rel="shortcut icon" href="{% static 'favicon.ico' %}">
+    <style>
+        :root {
+            --primary: #000000;
+            --bg-light: #f8f8f8;
+            --text: #333;
+            --gray: #666;
+            --line-height: 1.8;
+            --max-width: 900px;
+            --sidebar-width: 300px;
+        }
+        body {
+            color: var(--text);
+            background-color: #fff;
+            line-height: var(--line-height);
+        }
+
+        a {
+            color: var(--primary);
+            transition: color .2s;
+        }
+
+        a:hover {
+            color: darken(var(--primary), 10%);
+        }
+
+        /* 主容器:限制最大宽度并居中 */
+        .ui.grid.stackable.container {
+            max-width: var(--max-width);
+            margin: 2rem auto;
+            padding: 0 1rem;
+        }
+
+        /* 段落与标题留白 */
+        p {
+            margin-bottom: 1rem;
+        }
+
+        h2.ui.header {
+            margin-top: 2rem;
+            margin-bottom: 1rem;
+        }
+
+        /* 卡片式内容区 */
+        .eleven.wide.column {
+            background: #fff;
+            padding: 2rem;
+            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
+            border-radius: 4px;
+        }
+
+        /* 代码块滚动与样式 */
+        .pre {
+            overflow-x: auto;
+            padding: 1rem;
+            background: #fafafa;
+            border-left: 3px solid var(--primary);
+            margin: 1.5rem 0;
+        }
+
+        /* 顶部菜单去掉死板的灰色,增加一点阴影 */
+        .ui.top.fixed.menu {
+            background: #fff;
+            box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
+        }
+
+        /* 菜单项内边距更舒适 */
+        .ui.menu .item {
+            padding: 0.8rem 1.2rem;
+            font-size: 1rem;
+        }
+
+        /* 移动端:展开动画更顺滑 */
+        .ui.vertical.menu {
+            transition: max-height .3s ease-out;
+            overflow: hidden;
+        }
+
+        .ui.vertical.menu.collapsed {
+            max-height: 0 !important;
+        }
+
+        .ui.vertical.menu.expanded {
+            max-height: 500px !important;
+        }
+
+        /* 右侧 Archiv/Top 区域在大屏下保持粘性 */
+        .four.wide.right.floated.column {
+            position: sticky;
+            top: 80px; /* 根据你的导航高度微调 */
+            max-width: var(--sidebar-width);
+            margin-left: 2rem;
+        }
+
+        /* 调整归档和标签列表间距 */
+        .ui.list .item {
+            padding: .5rem 0;
+        }
+    </style>
 
     <style type="text/css">
         p > a > img {
@@ -144,7 +242,7 @@
 <div class="ui tablet computer only padded grid">
     <div class="ui top fixed borderless fluid huge menu">
         <div class="ui container">
-            <a href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
+            <a href="/" class="header item " >CyberSicko.net</a>
             <a href="/" class="item">主页</a>
 
             {% for item in category %}
@@ -164,7 +262,7 @@
 </div>
 <div class="ui mobile only padded grid">
     <div class="ui top fixed borderless huge fluid menu">
-        <a href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
+            <a href="/" class="header item " >CyberSicko.net</a>
         <div class="right menu">
             <div class="item">
                 <button class="ui icon toggle basic button">
@@ -235,12 +333,13 @@
 
             </div>
 
-            <form onsubmit="save()" method="post" action="/comment/add" id="comment_form" style="margin-top: 10px"
+            <form method="post" action="/comment/add" id="comment_form" style="margin-top: 10px"
                   class="ui form">
                 {% csrf_token %}
                 <input hidden="hidden" name="article_id" value="{{ article.id }}">
+                <input hidden="hidden" name="article_title" value="{{ article.title }}">
                 <h4 class="ui dividing header">发表评论</h4>
-
+                <input id="veryCode" type="text" name="veryCode" hidden="hidden">
                 <div class="field">
                     <div class="two fields">
                         <div class="field">
@@ -328,19 +427,34 @@
                     <a class="item" href="/date/{{ record.datetime }}?page=1">{{ record.datetime }}
                         ({{ record.count }})</a>
                 {% endfor %}
-
             </div>
+
             <h4 class="ui header">Top</h4>
             <div class="ui list">
                 {% for tag in tags %}
                     <a class="item" href="/tag/{{ tag.name }}?page=1">{{ tag.name }} ({{ tag.count }})</a>
                 {% endfor %}
-
             </div>
         </div>
     </div>
 </div>
-
+<div class="ui mini test modal">
+    <div class="header">
+        请输入验证码
+    </div>
+    <div class="content">
+        <p id="veryCodeSet"></p>
+        <div class="ui input">
+            <input id="veryCodeInput" type="text" placeholder="请输入验证码">
+        </div>
+    </div>
+    <div class="actions">
+        <a onclick="submitComment()" id="confirmBtn" class="ui positive right labeled icon button">
+            确认
+            <i class="checkmark icon"></i>
+        </a>
+    </div>
+</div>
 
 <footer style="margin-top: 50px" class="ui secondary segment">
     <div class="ui two column stackable grid">
@@ -382,15 +496,47 @@
         }
     }
 
-    function save() {
+    function submitComment() {
+        var veryCodeInput = $("#veryCodeInput").val()
+        $("#veryCode").attr("value", veryCodeInput)
+        document.getElementById("comment_form").submit()
+    }
+
+
+    document.getElementById("comment_form").addEventListener("submit", function (event) {
+        event.preventDefault(); // 阻止表单提交
+        // 表单验证逻辑
+        var veryCode = $('#veryCode').val()
+        if (veryCode === undefined || veryCode === '') {
+            getVeryCode()
+            return
+        }
         var data = $('#comment_form').serializeArray();
+
         localStorage.setItem('nick_name', $('#nick_name').val())
         localStorage.setItem('email', $('#email').val())
         localStorage.setItem('website', $('#website').val())
         return true
-    }
+        // 其他处理逻辑
+    });
+
 </script>
 <script>
+
+    function getVeryCode() {
+        $.ajax('/comment/getVeryCode', {
+            method: 'get',
+            contentType: false,
+            processData: false,
+            success: function (res) {
+                $('.mini.modal').modal('show')
+                $("#veryCodeSet").html("请输入数字: " + res.data)
+            },
+            error: function (err) {
+            }
+        })
+    }
+
     $(document).ready(function () {
         $(".ui.toggle.button").click(function () {
             $(".mobile.only.grid .ui.vertical.menu").toggle(100);

+ 2 - 2
templates/category.html

@@ -98,7 +98,7 @@
                                         <div class="summary"><a>
                                             {{ item.user__first_name }}{{ item.user__last_name }}</a>
                                             发布了{{ item.category__name }}:<a
-                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a>
+                                                    href="/article/{{ item.title }}.html">{{ item.title }}</a>
                                           {% if  is_login %}
                                                 <a title="置顶" href="/article/show_show_index/{{ item.id }}"><i class="icon home"></i></a>
                                             {% endif %}</div>
@@ -121,7 +121,7 @@
                                         <div class="summary"><a>
                                             {{ item.user__first_name }}{{ item.user__last_name }}</a>
                                             发布了{{ item.category__name }}:<a
-                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a>
+                                                    href="/article/{{ item.title }}.html">{{ item.title }}</a>
                                           {% if  is_login %}
                                                 <a title="置顶" href="/article/show_show_index/{{ item.id }}"><i class="icon home"></i></a>
                                             {% endif %}</div>

+ 2 - 2
templates/index.html

@@ -25,7 +25,7 @@
                                         <div class="summary"><a>
                                             {{ item.user__first_name }}{{ item.user__last_name }}</a>
                                             发布了{{ item.category__name }}:<a
-                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a>
+                                                    href="/article/{{ item.title }}.html">{{ item.title }}</a>
                                             {% if  is_login %}
                                                 <a title="取消置顶" href="/article/cancel_show_index/{{ item.id }}"><i class="icon cancel"></i></a>
                                             {% endif %}</div>
@@ -49,7 +49,7 @@
                                         <div class="summary"><a>
                                             {{ item.user__first_name }}{{ item.user__last_name }}</a>
                                             发布了{{ item.category__name }}:<a
-                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a>
+                                                    href="/article/{{ item.title }}.html">{{ item.title }}</a>
                                             {% if  is_login %}
                                                 <a title="取消置顶" href="/article/cancel_show_index/{{ item.id }}"><i class="icon cancel"></i></a>
                                             {% endif %}</div>