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.content|safe }}
{% endif %} + +