Getting Started With Django

It's a starter kit for ones who want to work with Django and python.

Django is an open source web framework that enables you to build clean and feature rich web applications with minimal effort needed and in no time. It is written in Python; a general purpose language; but is well suited for developing Web Applications. Django loosely follows a model-view-controller (MVC) design pattern, which greatly helps in building clean and maintainable web applications. For introduction purposes, you’ll read about the following,

  • The MVC pattern in web development.
  • Why we should use Python.
  • Why we should use Django.

The MVC pattern in web development

Web development was a tedious task involving the use of CGI for interfacing external programs with the web server. It was not only difficult but also required a separate copy of the program to be launched for each request. Quite overwhelming right?

A few years ago, Model-View-Controller came into the picture for web-based applications. It separates data (model), user interface (view), and data handling logic (controller), so that one can be changed without affecting the other; benefiting the designers and developers to work on the same application without affecting and worrying about each other’s job. SWEET.

Why Python?

Python is a general purpose programming language. Although it is well suited for a wide variety of applications, it doesn’t leave the web behind. It’s a clean and elegant syntax plus the support of a large library of standard and contributed modules means it technically covers everything from parallel processing to accessing a file. Also its object-oriented model makes it suitable for MVC style development.

If you’re like many people (yeah I’m in too) who first started with C and Java, the fact that Python is easy to learn gets you to jump to the conclusion that python is not that necessary of a programming skill. ‘I BEG TO DIFFER’. Python can actually push you to great heights whether it comes to landing a job or getting to taste the hottest trend in tech like Big Data, Machine learning etc.

The reason I’m falling in love with Python is because it’s hard to mess up. The beauty of Python; besides its simplicity; is its readability, versatility,it's  fast to write and there’re no ‘Black Holes’ in its design.

As far as the performance is concerned, Python runtime environment is one of a kind. It is known to be the fastest and stably supports most of the famous web servers through modules. Python is also available for all major platforms: Windows, Mac, UNIX/Linux.

Why Django?

Probably as soon as MVC pattern developed its roots into web development, Python provided quite a few choices for the web frames. Django is one of the available frameworks for Python but the question is; what sets it apart from other frameworks and what makes it popular in the Python community?

If you’re a perfectionist but need to meet those deadlines (yeah I hate them too. Nobody likes the deadlines anyway. Right?) Django is the best option for you as everything falls in place and in no time. It was specifically made for people who want clear code and a good architecture for their applications. Below are a few more reasons to consider Django.

Integration

Django’s components were meant for integration, reusability and speed from the start. It started as an in-house framework for managing a series of news oriented websites but later its code was made public and Django team contributed its development using the Open Source model. So because of its roots, it provides tight integration between components.

ORM

Django comes with Object-Relational Mapper (ORM) which is more like a connection between data model and database engine. So even if you’re no master at SQL only the basics would work perfectly fine with it. Also migrating data from one database engine to another is just a matter of changing a configuration file.

URL Design

Django provides its users the convenience and flexibility allowing URL to be mapped using regular expressions. You can also define functions to handle each pattern. That means it works the both way; user as well as search engine friendly.

Administration interface

One of the coolest things about Django is that it provides you a ‘built in’ administration interface. It’s just not only ready to use but also highly flexible and customizable.

Security

Security is the main concern for everyone nowadays. But no worries Django has you covered with its features. It includes advice on securing a Django-powered site including Cross site scripting (XSS), Cross site request forgery (CSRF), SQL injection, Clickjacking, SSL/HTTPS protection and Host header validation. You can read more about the security features at https://docs.djangoproject.com/en/2.0/topics/security/

Internationalization and localization

As the name suggests, Django facilitates a single web application to offer its content in different languages and regions of the world.

Another cool thing is that Django has huge community support and also extensive documentation which has your back in case you run into some mess. No doubt this is why those ‘High Load’ and popular projects like Pinterest and Instagram rely on Python and Django in particular to provide users with stable, fast access.

That’s it for the introduction part. Now let’s go ahead and see how to set the things up.

Set up

In order to get started with Django, you’ll need the following,

  • Installing Python
  • Installing Django
  • Installing a database system

Just to be clear as being a Windows user, I’ll only be covering installation on Windows.

Installing Python

Django is written in Python so the very first step towards our development setup is the installation of Python. Visit http://www.python.org/download/ and download the latest version of Python according to your system. Next, execute the .exe file and follow the instructions. The graphical installer will guide you throughout. After the installation, just to check, open the command prompt and enter the following command

  1. python --version
    If everything worked out perfectly, you should see the version of Python you installed.

     
    Figure 1: Installing Python

Something about pip

pip is like the package manager for Python. It will make installing and uninstalling Python packages (such as Django) very easy. To get pip on your machine just enter the following command at command prompt.

  1. Python –m pip install –U pip

 It will either let you know that pip is up to date or update it for you. For me, it is up to date so it shows something like this.

 
Figure 2: Installing pip

 

Installing Django

Now with everything set up, installing Django is super easy. Just enter the command

  1. pip install Django

The above command will get the latest version of Django for you. Or if you want to install a specific version of Django, modify the command like this:

  1. pip install Django==2.0.4

Easy right?

Time out Exception

Watch out. If the above procedure doesn’t seem to work for you (as for me you see so many freaking red lines in response indicating time out exception). First thing; Don’t panic. It happens because there is too short a default timeout set for pip by default. All we need to do is change this default timeout and we’re good to go. Enter the following command:

  1. pip --default-timeout=60 install django //setting the default timeout to 60 (1 min)

or

  1. export PIP_DEFAULT_TIMEOUT=60
  2. pip install django

You can set this default timeout to whatever suits you. If 1 min doesn't work for you then you can increase it.

If everything worked, go ahead, open the Python shell and get the version of Django. It’s not mandatory but just for checking purposes.

 
Figure 3: Installing Django

Installing Database system

Django supports various database engines like MySQL, PostgreSQL, MS SQL Server, Oracle, and SQLite. But you don’t need to worry about getting your hands dirty. If you are using the latest version of Python, you won't need to install anything, since it comes with the SQLite database management system contained in a module named sqlite3. For the sake of simplicity, let’s just keep it this way.

Now your development setup is all done. Next step is of course creating your First Project in Django which I’ll be covering next.