Creating Django Application and Introduction to Django Models(Part IV)

In this tutorial, we are going to learn how to make Django Application and have a look at what Django Models are.

This tutorial is a part of our series on Creating Blog Website using Django.

Link to the Previous tutorial:

Tutorial series on creating a basic Blog Application using Django

Starting a Django Application

To keep all the files and folders tidy, we are going to create a separate application for our blog in our Project.

To create an application, open up Terminal/Console in same directory containing manage.py, activate the Virtual Environment and then run the following commands:

For Windows :



python manage.py startapp blog1

For Linux/Mac OS :

python manage.py startapp blog1

where ‘blog1’ is the name of Application.

Now, you can see a folder with the same name as your application in the virtual environment directory with a file structure similar to :

codespeedy_venv
├── blog1
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │ └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── site1
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── requirements.txt

After that, we need to tell Django to use the newly created application. For that open site1/settings.py and in the INSTALLED_APPS section and an item ‘blog1‘, at the last of the list. It should look something like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog1',
]

Learn,

Django Objects and Models

There is a concept called Object Oriented Programming. In this rather than monotonously writing code in a single sequence, we model things and define how they interact with each other.

Objects are a collection of Methods and Properties. For example, let us say for our Blog post we need properties including Author’s name, Title , Published date and the content of the post. As for the methods, we will create a method publish which will put the posts on our website.

Post
--------
title
text
author
published_date

Now, a Model in Django is a special kind of object which can be saved in the database.

We can further think of model as a spreadsheet containing some rows and columns, which further provides a framework for creating more similar type of objects.

That’s it for now ! We will continue with creating Models in the next tutorial.

Feel free to comment down any doubt you face understanding the above steps.

Have a look at some other posts:

How to implement Dijkstra’s shortest path algorithm in Python

Membership and Identity Operator in Python

Leave a Reply

Your email address will not be published. Required fields are marked *