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:
- Basic concepts of Python
- HTML
- CSS
- Bootstrap
- 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:
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’
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:
Our models class forum will store:
- Customer name
- Customer email
- The topic of the forum
- Reference links
- Description
- Creation date
Discussion class is a child class of forum that stores views from different users. It has just two fields-
- 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.
- 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:
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)