Starting Django Project

Django is a web development framework for python. It is a highly scalable and secure framework. Big companies like instagram, Pinterest, , Disqus, Mozilla, etc use this technology.  

Why do you want to use a framework like django?

In web development, building a website needs to perform various tasks like fetching data from the database, handling views, rendering proper URLs, etc. And, also the application should be secure. Building applications from scratch could lead to a lot of security issues and you will be spending a lot of time building features that a framework provides out of the box. Using a framework helps to build web applications rapidly and also increases the chances of making applications secure. Django is a secure, fast, and reliable web development framework that is written in a python programming language.

Creating a django project

Before creating a project you need to have django package installed using pip, we will install all the dependencies used in our program

Run these commands in your terminal to install dependencies in this project.

pip install django

pip install django-ckeditor

pip install pillow

pip install django-crispy-forms

 

After installing dependencies you application looks like this

After installing dependinces you are ready to start a project using django-admin command, run following command in terminal this will create a project it has following syntax:

django-admin startproject <project_name>

Let’s say you want to create a project named portfolio_blog, you can write command

django-admin startproject portfolio_blog

It will create a project named prortfoli_blog as shown in this screenshot below:

Running your django application 

Now that you have a django project created you can run your django project using following command:

1. First go into the protfolio_blog folder using cd command:

cd portfolio_blog/

2. After you are inside portfolio_blog , run the runserver command as :

 python manage.py runserver

Now once you run this command you will see following screenshot:

This runserver command performs 2 steps:

  • It creates a database,┬áby default Django uses SQLite database.
  • .Runs the server

You might see unapplied migration in red , don’t worry about that you can fix that later.

Now you can open browser and go to http://172.0.0.1:8000 or localhost:8000, and you can see your django application running.

Next, part is applying migration to our application

Applying migrations 

Migrations are a feature of Django, migrations files are mapping of Django models to database schemas. There is two important commands in migrations first is makemigrations and the second is migrate  .
1. makemigrations – it creates migration files, these are files inside migrations/ folder in your app, you will see this later.

2. migrate – this command executes the migration files changes to the actual database. 

Django supports multiple databases, you can create migration files and can easily switch between different database. For, instance in development, you can create an application in SQLite database, and in production the same code could be used to support Postgres database

Let’s run migration 

To run migrations you perform the following command, first, stop your running server and then run the following command

python manage.py makemigrations
python manage.py migrate

This command will have effects as shown below:

After running this migration you can run your python manage.py runserver command and your warnings about migrations are gone.

In next post, structure of django project will be discussed in detail.

Leave a Reply

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