How To Build A Forum Website From Scratch Using Django - Django Comment System

How To Build A Forum Website From Scratch Using Django - Django Comment System

What is a Discussion Forum?

Discussion Forum is nothing but a place (in our case a website) that enables us to post our views over a certain topic publically and also allows us to see other’s opinions on the same. Let’s see how we can actually make this interesting Django project.

To make it more interesting, we’ll allow the user to post anonymously.

About the Discussion Forum Project

We’ll develop this interesting python project using a very popular python framework: Django which provides many inbuilt functionalities and thus make our work much simpler and less time-consuming.

Django is a Python-based free and open-source web framework and we can use it to develop any kind of project be it an E-commerce website or a simple Discussion Forum.

Project Prerequisites

To implement this project we need to know the following:

  1. Basic concepts of Python
  2. HTML
  3. CSS
  4. Bootstrap
  5. Django framework

If you don’t know much about Django, don’t worry we will start from scratch

To install the library, you can use pip installer on the command line:

pip install django

Download Discussion Forum Project Code

Before proceeding ahead, please download the source code of Discussion Forum Project: Discussion Forum in Python Django

Steps to Build the Project – Discussion forum

1. Creating the project and app:

Now we will create a new project, DataFlair_discsnForum and an app inside it named ’Discussion_forum’

django-admin startproject DataFlair_discsnForum
cd DataFlair_discusnForum
django-admin startapp Discussion_Forum

2. Creating the Database (models.py):

Let’s create the databases in models.py which is inside the app folder. The class will be a subclass of model class defined in models module, and we can define as many fields as we want in here and by default all fields are mandatory, to change this default behavior of Django, add to the specific field ‘null=True’ as seen in the below code.

Code:

from django.db import models
 
#parent model
classforum(models.Model):
name=models.CharField(max_length=200,default="anonymous")
email=models.CharField(max_length=200,null=True)
topic= models.CharField(max_length=300)
description = models.CharField(max_length=1000,blank=True)
link = models.CharField(max_length=100 ,null =True)
date_created=models.DateTimeField(auto_now_add=True,null=True)
 
def__str__(self):
returnstr(self.topic)
 
#child model
classDiscussion(models.Model):
forum = models.ForeignKey(forum,blank=True,on_delete=models.CASCADE)
discuss = models.CharField(max_length=1000)
 
def__str__(self):
returnstr(self.forum)

Our models class forum will store:

 

  1. Customer name
  2. Customer email
  3. The topic of the forum
  4. Reference links
  5. Description
  6. Creation date

Discussion class is a child class of forum that stores views from different users. It has just two fields-

  1. The forum which is a foreign key (Provide a many-to-one relation by adding a column to the local model to hold the remote value). It helps in maintaining a record of which opinion belongs to which forum.
  2. Discuss – It actually stores the opinion

We are using simple model fields for that purpose. The __str__() method will return the string representation of the object. These are simple Django concepts.

We are using Django inbuilt database SQLite so we need not add anything to our settings.py file. If you are using any other database don’t forget to change your database settings in settings.py file.

3. Creating and updating models: forms.py

This is required for creating, updating various models that we have created in models.py (for CRUD functionality). We will be using inbuilt Django forms so we don’t have to code much.

Code:

from django.forms import ModelForm
from .models import *
 
classCreateInForum(ModelForm):
class Meta:
model= forum
fields = "__all__"
 
classCreateInDiscussion(ModelForm):
class Meta:
model= Discussion
fields = "__all__"

It just contains two forms one for creating a new forum and one for adding views to the existing forum, which is clear from the models which we are using in Meta class (a metaclass is used to define an extra option for a model or form so that other classes within the web app know the capabilities of the model)

 

 

 

Post Code : NTItTWF5IDA1LCAyMDIy

photo

Sonjoy Bhadra

Python | Django | Laravel | 12Years Experience


1683

Views

104

Following

42

Posts

Popular posts

The Latest