When I decided to move all of my contents from WordPress to Django, one of my main concerns was to decide how to build a blog application using Django. Even though it seemed complicated at the beginning, it was a very simple task for me to build a blog application for my new web page.
Hence, I wanted to create this article to share with you the code that I created for the blog application and hopefully, you will find it useful. Absolutely there are still many things that are missing or I could do in a different way, so please let me know in case you have any suggestions. This is the main advantage of having open-source code since everyone has a different approach and the project might develop really well in comparison to one person’s involvement.
You need to follow the steps to install a blog application on your computer. Just to make sure that you have something to view inside of the application, I also included db.sqlite3, which includes a few posts, comments, and categories. In case you would like to see the live version of my blog application, you can always use the LINK and check out.
Below you can see the main view of my live application:
The first step you need to do is to clone the repository I created in GitHub:
Then you need to create a new virtual environment in the desired path and activate:
virtualenv -p python3 BlogAppEnv
Then navigate to the project directory you cloned from GitHub and install the requirements just to make sure that you have all the required packages installed:
pip install -r requirements.txt
Now you need to make migration and migrate, so please go to the directory of the project and use the commands below:
python manage.py makemigrations
python manage.py migrate
Within the database, as I mentioned, you will have sample posts, comments, and categories, which you can edit from the admin page. I also included the admin page within the main page.
You can initially use these credentials:
Username : superuser
Password : 123456
or you can simply create a superuser for yourself using the following command:
python manage.py createsuperuser
After completing all these steps, you can now run your application:
python manage.py runserver
The logic of the application is really simple:
- You have the main view, where you can see all of the posts that have assigned status visible. In case you will allow users to write a post in your blog application, the post will not be visible initially until you change the visible status to True. For this moment, this can be done only through the Admin page. I included visible status for posts just to make sure that the content I received from a user can be published on my web page.
- The same rule is applicable for comments. When a comment is submitted, the visible status is set automatically to False and you need to set it to True to make them visible within the Post page that the comment is related to.
- There is a filter created in the main view, which includes only the categories listed on the page at the moment. Even though you might have more categories in your Category table, they won’t be visible unless they are used in viewed posts.
- There is a separate view created and the user can view all of their posts even when they are not approved yet. The status of the post (published or not published) is visible on this page within the corner of the list element.
- There is also a view, in which the user can edit the post that was created before. The edit option is only available to the owner of the post and all users, who have superuser status in the application.
- I added a Like button so that users can click and show their appreciation. Like button is triggering the view, which increases the like number of the post. This number will be always available on the right side of Like button of the related post.
- An additional view was created just for approving the posts that were written by page users. This view changes the status to visible and locks the post for editing for all users except for superuser.
- Within the project, I included also TinyMCE, which makes it easier to edit the content. However, the link is created for me so please register to TinyMCE and get your own link. Otherwise, you will always receive a notification that your domain is not registered, however, you can still ignore this notification and use the editor.
I have a separate application created to handle notifications. Both user and superuser receive a message from the system once a post or a comment is submitted. Then superuser can approve or delete if necessary.
In case you have any questions, please feel free to use the link Ozan.pl Contact to reach me out.