sounak98's Blog

Creating Reddit Styled Comment System with Django

sounak98
Published: 06/28/2019

Okay, so you are probably blogging in this platform and have used the comment system once in a while. Those who have will notice that the comment system is pretty much like reddit without the upvote and downvote feature. This is actually pretty simple to create and can be created using only one django template which calls itself recursively! Yes you heard that right, recursing on django templates. It might sound pretty fancy, but is basically a very simple idea.

So let's begin with the model. We create a simple django model with the following fields

  • user (a foreign key to user referring to the author of the comment)
  • article (a foreign key to article referring to the article on which the comment was made)
  • content (the content of the comment)
  • parent (a self-referential field referring to the parent comment, and is a root comment if there is no parent)
  • created_at (the time and date of comment creation)

Once the model is made, we can create the migrations and migrate the database using the commands `python manage.py makemigrations` and `python manage.py migrate`. Now that the model is created and the database is migrated, we need to create views for addition and deletion of comments. We can also create a view for editing comments but that is not what we will discuss in this article. So the add view should be a POST view which takes an `user_id`, `article_id`, `content` and `parent`. Once we validate these data, we can create a new `Comment` using

Comment.objects.create(user=User.objects.get(id=user_id), article=Article.objects.get(id=article_id), content=content, parent=parent)

Make sure that `parent` is `None` if there is no parent comment.

Okay we are almost done. We just need to work on the template now.

So we will render the template with the context variables `root_comments` which will have all the root comments of the article. Now we will create another template called `comments.html` which will be passed some variables called `comments`, `user`, etc from the driver template. It will look something like this

{% include "aldryn_newsblog/includes/comments.html" with comments=root_comments article=article user=user csrf_token=csrf_token only %}

This `comments.html` will run a for loop to display all the comments and with each iteration it will also call the same template recursively like this

{% include "aldryn_newsblog/includes/comments.html" with comments=comment.replies.all parent=comment article=article user=user csrf_token=csrf_token only %}

We need to use the keyword only so that only these variables are passed and not the parent template variables which will override the correct ones. So, we will also need to pass some variables like `user`, `crsf_token`, etc. Also, we need to create a form which will POST to the add comment view with every template to reply to the parent comment in that template.

We can also implement a bunch of other features like hiding the reply threads, adding recaptcha verification, etc. The code can be found on this repo. Feel free to use it or take help from the same.

That's it for today. Hope it helps.

View Blog Post

Weekly Check-In #5

sounak98
Published: 06/25/2019

What did I do this week?

Finished up with the suborg application form which when filled out by a user sends out a notification to the admin. Then the admin can review it and send messages to the user. The user can change the application as per the instructions and get the application accepted.

Also now anyone can create an account using their email. This is a new feature that has been implemented.

What's coming up next?

Personalised notifications and dashboard for users

View Blog Post

Weekly Check-In #4

sounak98
Published: 06/25/2019

What did I do this week?

  • Added build automation using Travis CI
  • Started writing unit tests for the application
  • [MAJOR] Started working on the Suborg Application

What's coming up next?

  • Finishing up the Suborg Application flow
  • Write more tests
View Blog Post

Weekly Check-In #3

sounak98
Published: 06/09/2019

What did I do this week

  • [MAJOR] Auto creates GSOC@PSF Google Calendar from timeline, events and due dates
  • Add the new management command googleapiauth for authenticating the Google API
  • Fixed reminder notification bugs
  • Send notifications for comments
  • Refactored models
  • Minor updates on cleaning up the article contents
  • Updates blog count when an article is created and not updated

What's coming up next

  • Create the SubOrg Form

Did I get stuck anywhere

No, nothing as such.

Thanks for staying till the end.

View Blog Post

Weekly Check-In #2

sounak98
Published: 06/04/2019

Will keep it short and to the point this week.

What did I do this week

  • [MAJOR] Added feature for reminding students and mentors of missed blogs
  • Added functionality for reviewing blogs for admins
  • Fixed editing blogs (using a different field which didn't need separate permission, and thus also had to migrate data from the all the older blogs)
  • Add publish/unpublish feature for a student to display or to hide blogs from the main site

What's coming up next

  • Automatically create google calendar from timeline
  • Fix reminder notification bugs
  • Create comment notifications
  • Fix more bugs as they come up

Did I get stuck anywhere

Nothing major.

Thanks everyone!

View Blog Post