How to Create a Simple Blog Using Python as Well as Django
2022-07-17
Are you one of those people who has been dreaming of creating a simple blog to share your work? Does the idea of having that platform on your name to share your writing, has been making you feel excited to a great extent? However, the process seems vague because coding and web designing are a bit difficult. Is this the same with you?
We get you! You are a complete beginner here, and that is fine.
We assure you that you would celebrate the win here as a milestone. You need a little guidance and the knowledge of the right tools, and then you have your blog up and running in less than the expected time.
Sounds good? Then let us swim into the world of Django Python and get started to create a milestone.
What is a Django Application?
As per the explanation by Django software foundation, "A Django application is just a Python package that is specifically intended for use in a Django project. The Django framework may use common Django conventions, such as having models, tests, URLs, and views submodules.”
A Django Python blog application permits its users to create, edit, and delete posts. The homepage will show all blog posts, and there will be a detailed page dedicated to each post. This application can come to great use for you as it is efficient in making more higher stuff but making a blog is an outstanding step to initiate with as it will help you to get a good understanding of the framework.
Pre-Requisites
Django is an open-source web framework, which follows the Python language, and also the model-view-template architectural pattern. Thus, you need to make sure that you have the latest version of Python installed on your machine.
Moreover, before we get into the steps of creating a simple blog, we hope that you have learnt about the Django framework in brief.
Quick Steps of the Tutorial
It is recommended that you use the virtual environments to keep your Django code-based assignments isolated and safe
You would see a Django added in your terminal. This is to show you that your virtual environment is activated in the virtual environment of Django Python
Install the latest version of Django with the command: pip install Django. This way it will download and install Django on your system and after that, you can verify the same by executing the django admin
Set up the project by creating a directory called mysite and navigate into it
Run the command django-admin startproject mysite to create a Django project
You will see your project structure on the screen with directories and python scripts
The next step is to create a Django application called a blog by navigate where manage.py exists and run the following command: cd mysite python manage.py startapp blog
You will see an app named blog in the project
Open your setting.py file and find the installed application there
Now define the data models for the blog. Once done with it, then you need to let Django know that you are going to use those models. You can do that by bringing changes in the settings file and by modifying the INSTALLED_APPS setting to the actual name of the module that has your models.py
You will have the access to everything in django.db.models.Models by using the subclass functionality
After that, your database model will be shown on the screen
Create an admin panel by using the command: python manage.py createsuperuser to create and manage posts
You will be asked to enter email, password, and user details there and run the development server again
The next step is to provide the login details for the superuser which will take you to the basic admin panel
Now to add the posts model, Visit the: blog/admin.py file and register the Post model there as follows: from django.contrib import admin from.models import Post admin.site.register(Post)
Save the file and refresh the page to see the models
After that, you can create your first blog post by clicking on add post and make sure that you fill the forms given there
Once you are done with filling the forms, you will be taken to the post list page showing you the message of your success
Now that everything is done, the next and last few steps include creating views, templates, and URL configurations, for the blog post and blogger list detailed pages. For that, you need to configure the template setting first. The brief steps are of configuration are as follows:
Create direct panels in the base directory
Open the file named as, settings.py
Below BASE_DIR , add the route to the template directory as, TEMPLATES_DIRS = os.path.join(BASE_DIR,'templates'),
Come back to settings.py and scroll to TEMPLATES
Add the newly created TEMPLATE_DIRS in the DIRS
Save it, and you are done with the configuration process to create views, templates, etc
The last step is to create the comments form to let your visitors comment on what you write.
Conclusion
We hope that investing time in reading this has been beneficial for you. We are happy that you have finally decided to create your blog using Django framework. We wish you luck to move forward with it with great strength!