Procházet zdrojové kódy

完成了评论的后端,接下来做前端

Shellmiao před 4 roky
rodič
revize
604c6caa38

+ 2 - 1
MyBlog/MyBlog/settings.py

@@ -38,6 +38,7 @@ INSTALLED_APPS = [
     'article',
     'userprofile',
     'password_reset',
+    'comment',
 ]
 
 MIDDLEWARE = [
@@ -103,7 +104,7 @@ AUTH_PASSWORD_VALIDATORS = [
 
 LANGUAGE_CODE = 'en-us'
 
-TIME_ZONE = 'UTC'
+TIME_ZONE = 'Asia/Shanghai'
 
 USE_I18N = True
 

+ 1 - 1
MyBlog/MyBlog/urls.py

@@ -19,10 +19,10 @@ from django.conf import settings
 from django.conf.urls.static import static
 
 urlpatterns = [
-    path('', include('article.urls', namespace='article')),
     path('admin/', admin.site.urls),
     path('article/', include('article.urls', namespace='article')),
     path('user/', include('userprofile.urls', namespace='userprofile')),
     path('password_reset/', include('password_reset.urls')),
+    path('comment/', include('comment.urls', namespace='comment')),
 ]
 urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

+ 6 - 2
MyBlog/article/models.py

@@ -3,6 +3,7 @@ from django.db import models
 from django.contrib.auth.models import User
 # timezone用于处理时间相关的事物
 from django.utils import timezone
+from django.urls import reverse
 
 
 # 博客文章数据模型
@@ -10,12 +11,15 @@ class ArticlePost(models.Model):
     author = models.ForeignKey(User, on_delete=models.CASCADE)
     title = models.CharField(max_length=100)
     body = models.TextField()
-    created = models.DateTimeField(default=timezone.now) #使用timezone.now()时 进行数据迁移会,有警告
+    created = models.DateTimeField(default=timezone.now)  # 使用timezone.now()时 进行数据迁移会,有警告
     updated = models.DateTimeField(default=timezone.now)
-    total_views=models.PositiveIntegerField(default=0)
+    total_views = models.PositiveIntegerField(default=0)
 
     class Meta:
         ordering = ('-created',)
 
     def __str__(self):
         return self.title
+
+    def get_absolute_url(self):
+        return reverse('article:article_detail', args=[self.id])

+ 6 - 3
MyBlog/article/views.py

@@ -1,13 +1,15 @@
 from django.shortcuts import render, redirect
 from django.http import HttpResponse
-from .models import ArticlePost
-from .form import ArticlePostForm
 from django.contrib.auth.models import User
 from django.contrib.auth.decorators import login_required
 from django.core.paginator import Paginator
 from django.db.models import Q
 import markdown
 
+from .models import ArticlePost
+from .form import ArticlePostForm
+from comment.models import CommentPost
+
 
 # 视图函数
 def article_list(request):
@@ -39,6 +41,7 @@ def article_list(request):
 
 def article_detail(request, id):
     articles = ArticlePost.objects.get(id=id)
+    comments = CommentPost.objects.filter(article=id)
     articles.total_views += 1
     articles.save(update_fields=['total_views'])
     md = markdown.Markdown(
@@ -48,7 +51,7 @@ def article_detail(request, id):
             'markdown.extensions.toc'
         ])
     articles.body = md.convert(articles.body)
-    context = {'article': articles, 'toc': md.toc}
+    context = {'article': articles, 'toc': md.toc, 'comments': comments}
     return render(request, 'article/detail.html', context)
 
 

+ 8 - 0
MyBlog/comment/forms.py

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

+ 18 - 1
MyBlog/comment/models.py

@@ -1,3 +1,20 @@
 from django.db import models
+from django.contrib.auth.models import User
+from django.utils import timezone
 
-# Create your models here.
+from article.models import ArticlePost
+
+
+class CommentPost(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()
+    created = models.DateTimeField(default=timezone.now)  # 使用timezone.now()时 进行数据迁移会,有警告
+    updated = models.DateTimeField(default=timezone.now)
+    total_views = models.PositiveIntegerField(default=0)
+
+    class Meta:
+        ordering = ('-created',)
+
+    def __str__(self):
+        return self.body[:20]

+ 8 - 0
MyBlog/comment/urls.py

@@ -0,0 +1,8 @@
+from django.urls import path
+from . import views
+
+app_name = 'comment'
+
+urlpatterns = [
+    path('post_comment/<int:article_id>', views.post_comment, name='post_comment'),
+]

+ 22 - 2
MyBlog/comment/views.py

@@ -1,3 +1,23 @@
-from django.shortcuts import render
+from django.shortcuts import render, get_object_or_404, redirect
+from django.http import HttpResponse
+from django.contrib.auth.decorators import login_required
 
-# Create your views here.
+from article.models import ArticlePost
+from .forms import CommentPost
+
+
+@login_required(login_url='/user/login')
+def post_comment(request, article_id):
+    article = get_object_or_404(ArticlePost, id=article_id)
+    if request.method == 'POST':
+        comment_form = CommentPost(data=request.POST)
+        if comment_form.is_valid():
+            new_comment_form = comment_form.save(commit=False)
+            new_comment_form.article = article
+            new_comment_form.user = request.user
+            new_comment_form.save()
+            return redirect(article)
+        else:
+            return HttpResponse('表单有错误,请重新填写')
+    else:
+        return HttpResponse('发表评论仅接受POST请求')

binární
MyBlog/db.sqlite3


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

@@ -48,6 +48,11 @@
                 </div>
             </div>
         </div>
+    <div class="row">
+        <div class="col-12 mt">
+
+        </div>
+    </div>
     </div>
     <script>
         function confirm_delete() {