Django Framework was designed to make your web developing tasks very easy and fast. It has a lightweight development server which makes your developing tasks easier. Django Framework also provides optional administrative feature for CRUD operations which will reduce almost half of your workload while developing projects.
Get started with Django Framework
In this post we will learn the basics about Django Framework. You will know how Django Framework works and the basic structure of this framework. We will also learn how to run the development server for starting your developing works.

Getting Started with Django Framework
Checking Django Installation
First, I am assuming that you have already installed Django on your system. So now we will check the Django version of the system.
For that, we will open the Terminal and execute the command:
django-admin version
Alternatively, you can run the following command:
python -c "import django; print(django.get_version())"
Another way for checking version is that you can go to the python console and then check the version of Django Framework.
import django django.VERSION
My console returned 1.7 so my Django version is 1.7 that means I am OK to create projects with Django Framework. If there is something wrong then your system doesn’t have Django installed. Here is a good lesson for installing Django on your system.
Creating a Project
Now, we will create our project using django-admin
command. Note that django-admin
or django-admin.py
both of these commands will work in most of the systems where Django Framework is installed, but in some cases both commands will not work and in that case you will have to find out which command works.
For creating a project open the command line and go to that directory where you want to create the project using the cd
command. Then run the following command:
django-admin startproject blog
This will create a Django project named blog. Now if you go to the directory you will see a new directory named blog has been created which is the root directory of your project. When you explore inside the blog project directory you will see initially there is one directory and one file. This directory is also named blog
and the file is named manage.py
( it will be the main file which will be needed throughout developing your project ).
Now as we have created our project, we can configure our project and start developing.
Configure Django project
Go to the root directory of your project and now go inside the blog directory. You will see there are 4 files and one of them named settings.py
which will be needed for configuring your project further.
At first we need to check our Database configurating. When you open the settings.py file find the DATABASE variable and you will see something like this:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
This is because, When you create a Django project then the default database engine is set to SQLite. SQLite is used because it is portable and doesn’t need any installation which makes your developing tasks easier. But you can use other Database engines also.
Here is a list of some common Database engines used in Django Framework:
Database | Engines |
---|---|
SQLite | django.db.backends.sqlite3 |
PostgreSQL | django.db.backends.postgresql_psycopg2 |
MySQL | django.db.backends.mysql |
Oracle | django.db.backends.oracle |
As this is our example project we are sticking with SQLite. So for now we won’t need to change any database configuration but when you go into production then you will have to switch to production grade databases like Oracle, MySQL or PostgreSQL because SQLite is not a database to be used on production level. In that case your database configuration will require to be changed corresponding to the Database you will be using.
Migrate database
Django Framework has some built-in applications which gets installed in every project by default. Some of the applications need to save data in database tables, for that when we create a Django project then initially we need to migrate the database to function properly. To migrate, go to the root of your project and run the following command:
python manage.py migrate
Running project with the Development Server
Now we will see our project with the integrated Development Server of Django Framework. It is a lightweight and standalone server which comes in default with Django. To run the the development server in you local system run the following command:
python manage.py runserver
Now you will see the following output in the console:
Performing system checks... System check identified no issues (0 silenced). September 30, 2014 - 09:01:29 Django version 1.7, using settings 'blog.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
If the console shows no error then if you go to http://127.0.0.1:8000/ you will see the default page of Django project. It will look like this:

Running Django on Development Server
If you want to change the server’s port, pass it as a command-line argument. For example, this command will start the server on port 3000:
python manage.py runserver 3000
Note: if you see in console that the server is running but you cannot access that with your local system’s IP then you can set the development server to run and listen on all public IPs with the following command ( you can access the project with your system’s real IP and port number in which the server is running):
python manage.py runserver 0.0.0.0:3000
So, if your server IP is 117.12.34.6 and you run this command then you can access your project with http://117.12.34.6:3000/ and anyone can access it from anywhere of the world.
I hope this lesson helped you somehow for beginning with Django Framework. In the next lesson we will discuss about the structure of a Django project.
The post Getting Started with Django Framework appeared first on JS Tricks.