Browse Source

完成了评论的前端

Shellmiao 4 years ago
parent
commit
7def3619f3

+ 11 - 0
.idea/MyBlog.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+  <component name="TemplatesService">
+    <option name="TEMPLATE_CONFIGURATION" value="Django" />
+  </component>
+</module>

+ 17 - 0
.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,17 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
+      <option name="ourVersions">
+        <value>
+          <list size="4">
+            <item index="0" class="java.lang.String" itemvalue="2.7" />
+            <item index="1" class="java.lang.String" itemvalue="3.7" />
+            <item index="2" class="java.lang.String" itemvalue="3.8" />
+            <item index="3" class="java.lang.String" itemvalue="3.9" />
+          </list>
+        </value>
+      </option>
+    </inspection_tool>
+  </profile>
+</component>

+ 6 - 0
.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 4 - 0
.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/MyBlog.iml" filepath="$PROJECT_DIR$/.idea/MyBlog.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 2 - 2
MyBlog/article/views.py

@@ -8,7 +8,7 @@ import markdown
 
 from .models import ArticlePost
 from .form import ArticlePostForm
-from comment.models import CommentPost
+from comment.models import Comment
 
 
 # 视图函数
@@ -41,7 +41,7 @@ def article_list(request):
 
 def article_detail(request, id):
     articles = ArticlePost.objects.get(id=id)
-    comments = CommentPost.objects.filter(article=id)
+    comments = Comment.objects.filter(article=id)
     articles.total_views += 1
     articles.save(update_fields=['total_views'])
     md = markdown.Markdown(

+ 2 - 2
MyBlog/comment/forms.py

@@ -1,8 +1,8 @@
 from django import forms
-from .models import CommentPost
+from .models import Comment
 
 
 class CommentPost(forms.ModelForm):
     class Meta:
-        model = CommentPost
+        model = Comment
         fields = ['body']

+ 34 - 0
MyBlog/comment/migrations/0001_initial.py

@@ -0,0 +1,34 @@
+# Generated by Django 3.1.1 on 2021-01-26 09:45
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('article', '0002_articlepost_total_views'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='CommentPost',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('body', models.TextField()),
+                ('created', models.DateTimeField(default=django.utils.timezone.now)),
+                ('updated', models.DateTimeField(default=django.utils.timezone.now)),
+                ('total_views', models.PositiveIntegerField(default=0)),
+                ('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comment', to='article.articlepost')),
+                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comment', to=settings.AUTH_USER_MODEL)),
+            ],
+            options={
+                'ordering': ('-created',),
+            },
+        ),
+    ]

+ 20 - 0
MyBlog/comment/migrations/0002_auto_20210126_2310.py

@@ -0,0 +1,20 @@
+# Generated by Django 3.1.1 on 2021-01-26 15:10
+
+from django.conf import settings
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('article', '0002_articlepost_total_views'),
+        ('comment', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RenameModel(
+            old_name='CommentPost',
+            new_name='Comment',
+        ),
+    ]

+ 1 - 1
MyBlog/comment/models.py

@@ -5,7 +5,7 @@ from django.utils import timezone
 from article.models import ArticlePost
 
 
-class CommentPost(models.Model):
+class Comment(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment')
     article = models.ForeignKey(ArticlePost, on_delete=models.CASCADE, related_name='comment')
     body = models.TextField()

BIN
MyBlog/db.sqlite3


+ 41 - 5
MyBlog/templates/article/detail.html

@@ -46,13 +46,49 @@
                         {{ article.body|safe }}
                     </p>
                 </div>
+                <hr>
+                {% if user.is_authenticated %}
+                    <div>
+                        <form action="{% url 'comment:post_comment' article.id %}" method="post">
+                            {% csrf_token %}
+                            <div class="form-group">
+                                <label for="body">
+                                    <strong>
+                                        发表评论
+                                    </strong>
+                                </label>
+                                <textarea type="text" class="form-control" id="body" name="body" rows="2"></textarea>
+                            </div>
+                            <button type="submit" class="btn btn-primary">发送</button>
+                        </form>
+                    </div>
+                    <br>
+                {% else %}
+                    <br>
+                    <h5 class="row justify-content-center">
+                        请<a href="{% url 'userprofile:login' %}">登录</a>后回复
+                    </h5>
+                    <br>
+                {% endif %}
+                <h4>共有{{ comments.count }}条评论</h4>
+                <div>
+                    {% for comment in comments %}
+                        <hr>
+                        <p>
+                            <strong style="color: pink">
+                                {{ comment.user }}
+                            </strong>于
+                            <span style="color: green">
+                        {{ comment.created|date:"Y-m-d H:i:s" }}
+                    </span>时评论道:
+                        </p>
+                        <pre style="font-family: inherit;font-size: 1em;">
+                    {{ comment.body }}
+                </pre>
+                    {% endfor %}
+                </div>
             </div>
         </div>
-    <div class="row">
-        <div class="col-12 mt">
-
-        </div>
-    </div>
     </div>
     <script>
         function confirm_delete() {