Django is a python-based framework that is commonly used to build a variety of web services-based applications. Even building complex web applications are possible with Django because it is an open-source framework. Moreover, it highly supports the backend functions of web applications. Today, it is one of the commonly used top web development languages. It is highly favored by most developers while developing web applications because of its simplicity, scalability, reliability, and flexibility. Some of the famous websites built with Django are The Washington Post, Spotify, Dropbox, National Geographic, etc.
Compared to other languages such as Laravel or Yii, Django has its own in-built functionalities and components that support many HTTP responses. It has the capability to develop a web application with clean design, security, and maintainability. This is one of the reasons why Django can be used to build effective blog applications.
What benefits does Django offer for developing an effective blog application?
Launch custom API
Typically, the interaction between the front end of an application with back end functionalities is the hard part in the development of any web application. Django offers microservices and it supports the REST framework. This has an in-built powerful library as well as follows Model View Template Pattern (MTP). With these features, customizing APIs to serve multiple functionalities between the front end and back end of a blog is easier.
Unlike traditional solutions offered by other languages, Django is an expert framework in providing instant responses to every interaction in real-time. Thus, live streaming through blogs, creating charts, allowing user interactions in blogs by exchanging data, handing sockets and connections asynchronously.
Build serverless blogs
Blogs built with custom APIs using Django can easily be deployed in the cloud platform. There is no need for any permanent infrastructure. There is no issue in scaling the data size either. Django is capable of pushing Web Server Gateway Interface (WSGI) into a serverless platform with its Zappa framework.
Apart from the above important benefits, there are more than Django offers in building an effective blog application. Some of those additional benefits are building dynamic blogs with just shorter versions of front code and also even embedding chatbots in the blog if necessary.
Now that you are familiar with some of the important benefits Django can bring in the development of a blog, let us see how to develop an effective blog with Django.
Get started with building a blog with Django
Make a note of pre-requirements
Django is a python-based framework and it is very important to ensure python is installed before the development process starts. Some systems still on run on Python 2 as it is commonly known as the legacy version. However today all applications are migrating towards Python 3 as it is the active version. So, make sure that the Python 3 version is installed in your system. If you need assistance in installing python, there are many free guides on installing Python out there online which are free and easily understandable.
Creating a virtual environment
To store a back-up of the blog project in case something goes wrong during development is always a smart decision. Thus, creating a virtual environment to isolate the blog application’s dependency is a good choice.
Creating a virtual environment for Mac
|mkdir djangocd djangopython3 -m venv myenvsource django/bin/activate|
Creating a virtual environment for Windows
|cd Desktopvirtualenv djangocd djangoScripts\activate.bat|
These command lines will show you that the virtual environment is activated in your system. If you don’t see it, there are many free guides online on how to create a python-based virtual environment. Check them out.
Setting up a virtual environment for Django
Now that you have created a virtual environment, the following step is to install Django and step up the environment to support it. This can be achieved with a single command line as follows. This command line also automatically installs the latest version of Django according to your virtual environment. So, you do not have to worry about manually installing the latest version of Django.
|pip install Django|
Starting the project
In your system, now it is time to create your own directory for building this blog application. In this case the directory name is “tutorial”. You can change the name as per your need.
|cd Desktopmkdir tutorialcd tutorial|
Creating a Django application
To create a Django app for your blog, run the following command.
|django-admin startproject tutorial|
This will create a new directory for tutorial with a specific project structure using python scripts.
l— turorial l l— __init__.py l l— settings.pyl l— urls.py l l— wsgi.pyl— manage.py
The next step is to navigate to manage.py to create your desired Django application to execute specific functionalities. In this case, the Django application is called “my blog”. You can change the name as per your desire.
cd mysitepython manage.py startapp myblo
You should end up creating a Django application with your desired name (“myblog” in this case) like this:
l— db.sqlite3 l— tutorial l l— __init__.py l l— settings.pyl l— urls.py l l— wsgi.pyl— manage.pyl__ myblog l— __init__.py l— admin.py l— migrations l l__ __init__.py l— models.py l— tests.py l__ views.py
Updating and informing Django of this new application is the next step. For this, you need to open settings.py and once you do, you will find some applications already installed as follows.
|INSTALLED_APPS = [ ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’,]|
To this section now you have to add your Django application like this:
|INSTALLED_APPS = [ ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’, ‘myblog’]|
After completing this step, migrate the new settings section back to the existing Django application with the command line as below.
|python manage.py migrate|
Finally, run the below command to test if your set up has been successfully achieved.
|python manage.py runserver|
Go to the URL address http://127.0.0.1:8000/ . Your browser should look like this.
If it does then congratulations, you have successfully completed your set up. It’s time to move on to higher levels of building your blog with Django.
Building a database model
In order to store data from your blog, you need a database to store all that. SQLite is commonly used to conduct Object Relational Mapping (ORM) to handle data for blogs built with Django.
The subclass django.db.models.The model can be used to add additional functionalities and fields to the blog database to store and publish posts. Following is just an example database model according to “mblog” in this case. You can configure the model according to your requirement.
|from django.db import models|
STATUS = ( (0,”Draft”), (1,”Publish”))
class Project(models.Model): title = models.CharField(max_length=100) description = models.TextField() technology = models.CharField(max_length=20) image = models.FilePathField(path=”/img”)
In this model, under the class models, creating a subclass of models.The model is written to build functions to give title, description to the content. The title is a short string and description is a large string. Technology is a string field but however, it can offer only a limited amount of content. The image field is the function that stores the path of the image that is to be stored for publishing in the blog.
The STATUS function is used to save drafts and published posts. This will be useful if you want to build additional templates for your blog application using Django in the future.
In this section, the final step is migrating the created database model to a new migration record to update your Django blog application of the made changes. Type the following command.
|python manage.py makemigrations python manage.py migrate|
Creating models for blog functions
In this model, you will see how to create functionalities for posting, commenting and creating categories for your blog using Django.
To create post and category functionality
|from django.db import models|
class Category(models.Model): name = models.CharField(max_length=20)
class Post(models.Model): title = models.CharField(max_length=255) body = models.TextField() created_on = models.DateTimeField(auto_now_add=True) last_modified = models.DateTimeField(auto_now=True) categories = models.ManyToManyField(‘Category’, related_name=’posts’)
To create comment functionality
|class Comment(models.Model): author = models.CharField(max_length=60) body = models.TextField() created_on = models.DateTimeField(auto_now_add=True) post = models.ForeignKey(‘Post’, on_delete=models.CASCADE)|
Note that the name of the category while creating the post functionality is as per your desire. The category is named as “posts” in this case. Once this model is built, again it has to the Django application has to be updated with the newly developed model. Follow the migration step by typing this:
|python manage.py makemigrations myblogpython manage.py migrate|
In this case “myblog” is added in the migration command line because the created functionalities are specific to myblog Django application. Replace “myblog” with the name of your Django application.
Creating administration model
To have control over your blog application and effectively manage it, developing an administration section is highly important. Follow the below steps to successfully build a fascinating admin section.
The first step is to type the following command to create a superuser functionality that comes with in-built admin features.
|python manage.py createsuperuser|
This command line will prompt you to enter the following details and this is what will appear. Enter details to each of the fields as per your desire.
|Username (leave blank to use ‘name’): admin Email address: firstname.lastname@example.org Password:Password (again):Superuser created successfully.|
Replace name in a username, e-mail address and password with your desired details. Once you complete this, you will see a statement that says “Superuser created successfully”. Note that you can change these details anytime later if required.
Next, go to the address http://127.0.0.1:8000/admin/ . Once you do, you should see something like this.
Type the following command to create appropriate authentication and authorization.
|from django.contrib import adminfrom blog.models import Post, Category|
class PostAdmin(admin.ModelAdmin): pass class CategoryAdmin(admin.ModelAdmin): pass admin.site.register(Post, PostAdmin)admin.site.register(Category, CategoryAdmin)
Note that this admin model is to serve as a tutorial for giving you an idea in developing a blog with Django application. In this case “myblog”. You can add any extra attributes, change, and functionalities as per your requirement for your blog. Do not forget the last two command lines in the admin model above because they are the ones that will register your admin model with the admin class. Once you complete this, you should see something like this in your browser after you navigate again to http://127.0.0.1:8000/admin/.
There are many more models and templates you can build for your blog application with Django such as creating views. Creating a template is not necessary unless you want to create a template that you want to use for all your blog posts and functionalities. Building a template is required if you want to maintain a similar structure for specific functionalities. If you want to create one, there are unlimited free guides on creating templates for blog applications with Django online too. Check them out for more ideas.
Play with creating your own blog application with Django
You do not need to be an expert developer in order to build an effective blog application with Django. All you have to do is to follow all the steps mentioned above. If you want more customization, tools, and functionalities, there are always various ideas to look out at through free online courses and guides. You can also always connect with the active community to discuss ideas and get help. In the end, Django is a trusted framework and building a blog application with it comes down to what you require.
Let us know if you have successfully created an effective blog application with Django.