From 7a8ab533d47a8debee715f6156d319df86dc1771 Mon Sep 17 00:00:00 2001 From: Vrinda Date: Wed, 9 Dec 2020 23:59:16 +0530 Subject: [PATCH] Added comments section in Blog --- mysite/blog/admin.py | 4 +-- mysite/blog/forms.py | 7 ++++- mysite/blog/migrations/0005_comment.py | 27 ++++++++++++++++++++ mysite/blog/models.py | 12 +++++++++ mysite/blog/templates/blog/post_detail.html | 21 +++++++++++++++ mysite/blog/views.py | 20 ++++++++++++--- mysite/db.sqlite3 | Bin 241664 -> 245760 bytes 7 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 mysite/blog/migrations/0005_comment.py diff --git a/mysite/blog/admin.py b/mysite/blog/admin.py index 2376212..a25c266 100644 --- a/mysite/blog/admin.py +++ b/mysite/blog/admin.py @@ -1,9 +1,9 @@ from django.contrib import admin -from .models import Post +from .models import Post,Comment # Register your models here. admin.site.register(Post) - +admin.site.register(Comment) diff --git a/mysite/blog/forms.py b/mysite/blog/forms.py index 0cabd14..c015e42 100644 --- a/mysite/blog/forms.py +++ b/mysite/blog/forms.py @@ -1,5 +1,5 @@ from django import forms -from .models import Post +from .models import Post,Comment class PostForm(forms.ModelForm): class Meta: @@ -13,3 +13,8 @@ class Meta: def __init__(self, *args, **kwargs): super(PostForm, self).__init__(*args, **kwargs) self.fields['image'].required = False + +class CommentForm(forms.ModelForm): + class Meta: + model = Comment + fields = ('name', 'body') \ No newline at end of file diff --git a/mysite/blog/migrations/0005_comment.py b/mysite/blog/migrations/0005_comment.py new file mode 100644 index 0000000..8d70a88 --- /dev/null +++ b/mysite/blog/migrations/0005_comment.py @@ -0,0 +1,27 @@ +# Generated by Django 3.0.8 on 2020-12-09 18:08 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog', '0004_post_favourites'), + ] + + operations = [ + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=80)), + ('body', models.TextField()), + ('created_on', models.DateTimeField(auto_now_add=True)), + ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.Post')), + ], + options={ + 'ordering': ['created_on'], + }, + ), + ] diff --git a/mysite/blog/models.py b/mysite/blog/models.py index 3c3015d..9e23ca0 100644 --- a/mysite/blog/models.py +++ b/mysite/blog/models.py @@ -37,3 +37,15 @@ def readTIme(self): Read_Time=get_read_time(self.content) Read_Time = Read_Time[2:-3] return Read_Time + +class Comment(models.Model): + post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments') + name = models.CharField(max_length=80) + body = models.TextField() + created_on = models.DateTimeField(auto_now_add=True) + + class Meta: + ordering = ['created_on'] + + def __str__(self): + return 'Comment {} by {}'.format(self.body, self.name) \ No newline at end of file diff --git a/mysite/blog/templates/blog/post_detail.html b/mysite/blog/templates/blog/post_detail.html index 5c7bafc..dfc3a5f 100644 --- a/mysite/blog/templates/blog/post_detail.html +++ b/mysite/blog/templates/blog/post_detail.html @@ -61,6 +61,27 @@

{{ object.title }}

{{ object.title }}

{{ object.content|safe }}

{% endif %} + +

+
+

Comments

+
+ {% csrf_token %} + {{ comment_form.as_p }} + +
+ + +
+ {{ comments.count }} Comment{{comments|pluralize }} + {% for comment in comments %} +
+

{{comment.body}}

+
by {{comment.name|capfirst}}
+
+ {% endfor %} +
+ {% endblock content %} diff --git a/mysite/blog/views.py b/mysite/blog/views.py index 4e1ed71..83ae6a9 100644 --- a/mysite/blog/views.py +++ b/mysite/blog/views.py @@ -1,12 +1,12 @@ from django.shortcuts import render, redirect, get_object_or_404 -from django.http import HttpResponse -from .models import Post +from django.http import HttpResponse, HttpResponseRedirect +from .models import Post,Comment from django.contrib.auth.models import User from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.views.generic import ListView, DetailView, UpdateView, DeleteView from django.contrib import messages from django.contrib.auth.decorators import login_required -from .forms import PostForm +from .forms import PostForm,CommentForm from django.db.models import Q from django.core.paginator import Paginator from django.http import JsonResponse @@ -76,6 +76,7 @@ def Profileview(request,name): def PostDetail(request, slug): post = Post.objects.filter(slug=slug).first() + comments=Comment.objects.filter(post=post).order_by('-id') post.view_count = post.view_count + 1 post.save() @@ -85,9 +86,22 @@ def PostDetail(request, slug): if objects.favourites.filter(id=request.user.id).exists(): fav = True + if request.method == 'POST': + comment_form = CommentForm(request.POST or None) + if comment_form.is_valid(): + body = request.POST.get('body') + name = request.POST.get('name') + comment = Comment.objects.create(post=post, name=name, body=body) + comment.save() + return redirect(request.path) + else: + comment_form = CommentForm() + context = { 'object': objects, 'fav': fav, + 'comments' : comments, + 'comment_form' : comment_form } return render(request, 'blog/post_detail.html', context) diff --git a/mysite/db.sqlite3 b/mysite/db.sqlite3 index 273aa35441126847fdb44a70650f190109f6c3e4..375ab9b9733fd305efd8b02853ae40c2efb0bfce 100644 GIT binary patch delta 2035 zcmah~ZER9k7`}%~xusBEu(x6@x?3t6voT(Y?s-Ilp*iF3(f_G2^IftIFdq7dF3 z&hwo2eb0HH^Pb~(RL76QxkgO~K@dCfulzavx|v5EAB-#m*-V|yQfjY+I-BoPdnnG@ zTt(CyJfn%!1k19H;jz^8w3tlWSR1>Yvu$VH4A<7qy4&qGi=)l$0V!A1B1oS=g-s44V~NPbmm$B0b?p?-!mprZ4#V_8;b^&w++%=#QMXHQuf zL}xGJu_AlJ+FI9KgHu+3gMdHbE4T@tfFrMFn?a+N);E^?tmdc4`kw!EvjXe{+{2N- zge&kiyb4Dl27AE^_Iwk&5j3EX67`M6@T<$R-oRDTtX$4gFZNX{e^P#}EGU=rO-z?E zutACpNHIGl&XrR&(^6zYil~?n(_(qDb!BpFGLoEFn{3fYQ8Q9RqR}$Vl>hr>O&zII zJjhb}tu=}VDsSy+Xs+$R!9G$LwvhrlwExh-#8ey~YQz=cId>u>^3F+KY?~dAAK(s8 z>hhb9{!vG2C@xE#Ym!hLE*M4CF4ohv#G=U~Z>X)6zwy?lg`VKkG$Xjf&8Ikug1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