Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #206] Added comments section for Blog #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 6 additions & 1 deletion mysite/blog/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from .models import Post
from .models import Post,Comment

class PostForm(forms.ModelForm):
class Meta:
Expand All @@ -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')
27 changes: 27 additions & 0 deletions mysite/blog/migrations/0005_comment.py
Original file line number Diff line number Diff line change
@@ -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'],
},
),
]
12 changes: 12 additions & 0 deletions mysite/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions mysite/blog/templates/blog/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ <h2 class="article-title">{{ object.title }}</h2>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content|safe }}</p>
{% endif %}

<br/><br/>
<hr>
<h2> Comments</h2>
<form method="post">
{% csrf_token %}
{{ comment_form.as_p }}
<input type="submit" value="Submit" class="btn btn-outline-success">
</form>


<div class='main-comment-section'>
{{ comments.count }} Comment{{comments|pluralize }}
{% for comment in comments %}
<blockquote class="blockquote">
<p class="mb-0">{{comment.body}}</p>
<footer class="blockquote-footer">by <cite title="Source Title">{{comment.name|capfirst}}</cite></footer>
</blockquote>
{% endfor %}
</div>

</div>
</article>
{% endblock content %}
20 changes: 17 additions & 3 deletions mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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)

Expand Down
Binary file modified mysite/db.sqlite3
Binary file not shown.