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}}

+ +
+ {% 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 273aa35..375ab9b 100644 Binary files a/mysite/db.sqlite3 and b/mysite/db.sqlite3 differ