In this post, we are going to see an overview of Django (as a tutorial) and setting up the environment to get started with building WebApps or Rest APIs.
Before we start setting up the environment we can have a look into the features Django brings to help with development.
2. What is Django?
Django is a free and open-source Python back-end web framework that removes the tedium of building websites by providing most of the required behavior. Django handles the majority of the HTTP request and response cycle (the rest is handled by the server Django runs on top of). Developers need only focus on processing the HTTP request, and Django provides tools to make even that easy.
3. Why should we learn Django?
It’s definitely the most complete, offering a wide range of features out-of-the-box, such as a standalone Web server for development and testing, caching, middleware system, ORM, template engine, form processing, interface with Python’s unit testing tools. Django also comes with battery included, offering built-in applications such as an authentication system, an administrative interface with automatically generated pages for CRUD operations, generation of syndication feeds (RSS/Atom), sitemaps. There’s even a Geographic Information System (GIS) framework built within Django.
4. Who’s Using Django?
This is a good question and an important aspect before starting to learn anything new. The biggest users of Django are Instagram, Disqus, Mozilla, etc.For more examples you can see the Django Sites database, they offer a list of over 5K Django-powered Web sites.
Let's start learning and write some code!
5. Setting up Developers Environment
Before we start developing always try to learn the setup and important packages needed.
Tip: Remember you cannot build anything if you dont know how to use the tool to build.
Before you can start learning how to use Django, you must first install some software on your computer. Fortunately, this is a simple three-step process:
- Install Python 3 (Yes! we are using the latest version).
- Install a Python Virtual Environment.
- Install Django.
If this does not sound familiar to you don't worry, In this post, let's assume that you have never installed software from the command line before and will lead you through it step by step.
5.1 Python 3 Installation & Setup Guide
Step #1: Download the Python 3 Installer
- Open a browser window and navigate to the Download page for Windows at python.org.
- Underneath the heading at the top that says Python Releases for Windows, click on the link for the Latest Python 3 Release - Python 3.x.x.
- Scroll to the bottom and select either Windows x86-64 executable installer for 64-bit or Windows x86 executable installer for 32-bit.
Step #2: Run the Installer
Once you have chosen and downloaded an installer, simply run it by double-clicking on the downloaded file. A dialog should appear that looks something like this:
Windows installation dialog
Important: You want to be sure to check the box that says Add Python 3.x to PATH as shown to ensure that the interpreter will be placed in your execution path.
Then just click Install Now. That should be all there is to it. A few minutes later you should have a working Python 3 installation on your system.
5.2 Install a Python Virtual Environment.
A Python virtual environment solves the problem by wrapping all the dependencies and environment variables that your new software needs into a file system separate from the rest of the software on your computer.
pip install virtualenv
Once virtualenv is installed, you need to create a virtual environment for your project by typing:
Once virtualenv has finished setting up your new virtual environment, open Windows Explorer and have a look at what virtualenv created for you. In your home directory, you will now see a folder called \myenv (or whatever name you gave the virtual environment). If you open the folder, you will see the following:
Activate the Virtual Environment
5.3 Installing Django
Now that we have Python and are running a virtual environment, installing Django is super easy, just type the command:
pip install django
This will install the latest version of Django
6. Starting a project
Once you've installed Python, Django, and (optionally) your database server/library, you can take the first step in developing a Django application by creating a project.
A project is a collection of settings for an instance of Django. If this is your first time using Django, you'll have to take care of some initial setup. Namely, you'll need to auto-generate some code that establishes a Django project: a collection of settings for an instance of Django, including database configuration, Django-specific options, and application-specific settings.
From your virtual environment command line, run the following command:
django-admin startproject mysite
This will create a mysite directory in your current directory. If you want to create your project in a directory other than the root, you can create a new directory, change into that directory and run the start project command from there.
7. THE DEVELOPMENT SERVER👏👏
A lightweight webserver is written purely in Python.
Let's verify your Django project works. Change into the outer mysite directory, if you haven't already, and run the following commands:
python manage.py runserver
You'll see the following output on the command line:
Performing system checks... 0 errors found
June 12, 2016-08:48:58
Django version 1.8.13, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Now's a good time to note: don't use this server in anything resembling a production environment. It's intended only for use while developing.
Now that the server's running, visit http://127.0.0.1:8000/ with your web browser. 👌👌
Django follows the MVC pattern closely, however, it does use its own logic in the implementation. Because the C is handled by the framework itself and most of the excitement in Django happens in models, templates and views, Django is often referred to as an MTV framework. In the MTV development pattern:
- M stands for "Model," the data access layer.
- T stands for "Template," the presentation layer.
- V stands for "View," the business logic layer.
To sum up this article, we learned about Django and setting up the development environment. We also know about the project setup in Django and running our first Django application.