Hacks by Ruddra

Make a Blog using Django Part 1

Please follow the official tutorial for creating your first django application: https://docs.djangoproject.com/en/2.1/intro/tutorial01/

“Making a blog using django” - is probably the most made tutorial of Django. This post is no different. But I made this in my own way using some of django’s built-in features so that less coding is required and making it more understandable with minimum packages to use.

To view the working source of this tutorial, check here at: https://github.com/ruddra/myblog

Before jumping to the main event, see if you have these installed in your pc:

  1. Python 3 installed in the computer. (Or python 2.7 if you prefer)

  2. Django 1.7 installed in the computer.

Django appears to be a MVC framework, but instead of using the name ‘Controller’, we call it as ‘View’ and ‘View’ as ‘Template’, Also Django is not a CMS. It’s a Web framework; it’s a programming tool that lets you build Web sites. Check here for details: SOURCE.

So as we stated before, django appears to be a MVC framework. MVC is a framework for building web applications using a MVC (Model View Controller) design:

  1. The Model represents the application core (for instance a list of database records).

  2. The View displays the data (the database records).(Here it is called “Template”)

  3. The Controller handles the input (to the database records).(Here it is called “View”) **(copied from here http://www.w3schools.com/aspnet/mvc_intro.asp)

Django manipulates data in the database using ORM(Object Relational Model). ORM saves you a lot of time by making the structure of the database, running CRUD(Create Read Update Delete) operations etc. Django ORM builds the structure of the database using the structure of the model. It means, the way you define the model, the way your database structure will be. A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table(more).

Now let’s start a project named myproject in desired directory using this command: django-admin.py startproject myproject. Then, create an app inside the myproject directory using python manage.py startapp myblog. So the structure should look like this:



This reusable app is going to be used for making the blog. More about reusable apps.

Append ‘myblog’ to myproject>myproject>settings.py’s INSTALLED_APP like:


Now we start by making a blog by making models. In this project, we are going to display Title, Body, Tags in each post. So for each content in a post, database’s table is going to need a field. So in our model, we are going to add those fields to myproject>myblog>models.py like:

from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255, null=True, default='')

    def __str__(self):
        return self.name

class MyBlog(models.Model):
    title = models.CharField(max_length=255)
    body = models.CharField(max_length=20000)
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.title

The reason for making these structure is that:

The model class Tag is going to be used for making/displaying tags. Now we have made model for blog, need to use ORM for making Database structure and add aditional data(Why migration is necessary? See here. So for that, go to myproject directory where manage.py resides and run:

$python manage.py makemigrations

$python manage.py migrate

$python manage.py createsuperuser --username=admin --email=[email protected]
#it will ask for setting a password

$python manage.py runserver
# for running the server

The third command for making a superuser in the system. The fourth command will run your project in this url: you don’t provide any specific ip/port). Or you can run like this python manage.py runserver and it will make your project run in and this is accessible from browser. The webpage will look like this when the project runs successfully:

So you have successfully ran the django site.

Now the database has been made and superuser has been created, so we go the next step, creating blogs. We are going to use django’s one of the most powerful and popular feature, django’s admin site. For making admin site visible and accessible, you need to add this lines to your urls.py (myproject>myproject>urls.py):

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover() #this line is for making model visible in admin site

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls), name='admin-site'),

This lines will let you access the django’s admin site using this url: (if you are running this project in localhost).

Now we need to modify the admin.py in myblog’s directory to register the app to admin site.

# Location myproject>myblog>admin.py
from django.contrib import admin
from django import forms

from myblog.models import MyBlog, Tag


Now your admin site will look like this:

So up-to admin site, the making of blog is complete. Next part will be provided in next post. Cheers!!