× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Configure Projects in Django Framework

In this tutorial, we are going to see how to configure our Django projects to deal with database native features.

So far we have created our first project ( in the previous article - http://mrbool.com/setup-and-first-application-with-django-web-framework/33520 ) and you must make sure that you are inside your virtual environment to further work on your Django Project. To recall, you can activate your virtual environment by entering the following command in your terminal:

source django/bin/activate

Next, we are going to look at settings in our application. “settings.py” file contains information about the database, timestamps and language code. This file is automatically generated when we create our Django project. We are going to use “sqlite3” database for our Django Web Application as it does not require any external things to be installed and it is very easy to setup.

So open up the file “settings.py” which is located inside django_test/django_test/settings.py. Listing 1 shows the code of the file with all the important things highlighted.

Listing 1. settings.py file inside our Django Project

 # Database 
# https://docs.djanqoproject.com/en/1.8/ref/settinqs/#databases 
         'default': { 
                   'ENGINE': 'django.db.backends.sqlite3', 
                   'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
# Internationalization 
# https://docs.djanqoproject.com/en/1.8/topics/i18n/ 

In the above figure, you can see that our database is “sqlite3” and language code is “en-us” and the time_zone is “UTC”. You can changes time_zone and language_code according to your requirements. Make sure to keep the database section as it is. If you want to use another database which you like, then you can visit the link (https://docs.djangoproject.com/en/1.8/ref/settings/#databases) and read the instructions to set up another database. Django will create a database file for your project with an extension “.db” and this database file will store all of your project data. You can choose any database you want and all databases require different setup procedures. You can refer to the above Django link for different databases.

Database Models

Next, we will learn how to start an application within our project. Our project can contain several applications. Applications are basically python packages and they can be simply considered as Python modules which can be imported into Django to assist our application. We will create an application called “articles” which will store all our articles in the database and display on our website.

Make sure you are inside the virtual environment and also inside our project folder. Write this in your terminal to create a new application:

python manage.py startapp article

Figure 1. Creating our article application

After writing the above command you can see the contents of the project and you will find that there is a new folder called “article” which is basically a new application that we have recently created.

Figure 2. Our article application folder

Here, you will see many different files. “__init__.py” file tells python that this folder is a package or module and can be imported in another python or Django file.”admin.py” file is used to activate the admin panel which can be used to alter our database from the web page rather than command line. “models.py” file contains classes and functions which consist of tables and data in our database. Classes are similar to tables and functions are similar to columns inside the database. “views.py” contains all different view functions that are specific to a particular URL on our website. In this article we will only deal with “models.py” file.

Open up the file “models.py” and write some code to create table inside our database.

Listing 2. Creating Article table

from django.db import models
# Create your models here.
class Article(models.Model):
         title = models.CharField(max_length = 200)
         body = models.TextField()
         pub_date = models.DateTimeField('date published')
         likes = models.IntegerField()

Inside models.py file, every class will be created as a table in the database and each and every instance of the class will be created as specific rows inside that table.

In the above code snippet you can see that class “Article” inherits from models.Model which helps to create representation of our data in applications. Our Article will have a title, body, pub_date( published date ) and likes( number of likes ). We have written code for the same. Now head back to your “settings.py” file and in the “installed_app” section add your new app i.e “article”. Listing 3 shows the code of the “settings.py” file after modification.

Listing 3. Modifying INSTALLED_APPS section in settings.py

 # Application definition

Now, we will create a database for the information that we have coded in the “models.py” file.

Open the terminal and write following statements shown in Listing 4.

Listing 4. Creating database for our project

python manage.py makemigrations
python manage.py migrate

After running these two commands, let’s open the database located inside our project folder with a SQLite Database Browser.

Figure 3. SQLite Browser

In the above figure, we can see that we have a table called “article_article” and it has all the content that we just wrote in the “models.py” file. You will see that “id” is automatically generated by Django and it is also the primary key for the table. So you do not have to take care of the id and primary key in Django.

The interesting thing is that we have not used any SQL commands. Django does that automatically for us.

So, till now, we have looked at “settings.py” file of our project which tells us the information about which database we are going to use for the project and also the information about timestamps and language code.

After that, we created our first app and written some code to create a table called “article_article”. “models.py” will contain all the code to create table and rows for the particular app. These models are then transferred to the “views.py” file and displayed to the user. But this is not the right time to learn about views in Django. First we will master the models and test our table in the Python Shell which is provided in the “manage.py” file.

Inside your terminal write this line:

python manage.py shell

This will open a python Django (similar to a Python shell ) shell which allows you to import modules from your Django app.

Let’s write some more commands in the terminal (Listing 5)!

Listing 5. Taking a look at number of articles

from article.models import Article
from django.utils import timezone

First, we are importing our Article class( table ) that we created in the “models.py” file and then we are importing “timezone” file from the “django.utils” which will keep track of the current date and time. “Articles.objects.all()” will provide all the objects inside our database but till now we have created no objects so it will be an empty list.

Let’s create some articles for our app. Inside the terminal write these lines:

Listings 6. Creating some articles

a = Article( title = "test_1" , body = "blah blah" , pub_date = timezone.now(), likes = 0 )
b = Article( title = "test_2" , body = "blah blah blah" , pub_date = timezone.now(), likes = 0 )
c = Article( title = "test_3" , body = "blah blah blah blah" , pub_date = timezone.now(), likes = 0 )

So, in the above piece of code, we have created three different articles and we have used “save()” method to save those articles in the database.

We can see inside the database browser that data is present, like in Figure 4!

Figure 4. Data inside our table

We already know that Django created “id” for us automatically, we can also view those id’s inside the shell. In Figure 5 we see the screenshot for the same.

Figure 5. ID’s for our articles

Now let’s again run the command “Article.objects.all()” and see what happens.

Figure 6. Total articles in our app

You can see that we have a total of 3 articles in our app and those are successfully added to the database!

Similarly you can add as many articles as you want to add. Remember one thing that this time we have added the articles in our database via command line but Django also provides us with an Admin Interface which lets you alter the database tables and add/remove data via a web page. We will look at this feature of Django in the future articles of this series. Stay Tuned!.


In this section, we looked at “settings.py” file of our project and now we know how to select a database for our project and select different time zones and language codes. We move further by creating our first application “article” and creating a table for that application in the database. We learnt how to create tables using classes and rows using objects in Django. We stored data in our database using the command line and we will also learn how to do the same via Django Admin Interface in the future. In this way, we can create as many articles as we want. If you are facing any problem then feel free to leave a question in the comments section.


Official Django Website => https://www.djangoproject.com/

Databases in Django => https://docs.djangoproject.com/en/1.8/ref/settings/#databases

CODING is my Passion first, hobby second and job third! I am Full Stack Developer from last two years and I love building scalable web applications and modern websites with new technologies.I started with my web development with ...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login