Django Tutorial #1: Getting Started

Please note that this post may contain affiliate links, and for every purchase you make, at no extra cost to you, a commission fee will be rewarded to me.

What is Python?

Python is a high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace.

The language’s design philosophy is summarized as:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Readability counts.

Python’s large standard library, commonly cited as one of its greatest strengths, provides tools suited to many tasks. Django is one of them which is designed for web development.

Why Django?

Django’s primary goal is to ease the creation of complex, database-driven websites. The framework emphasizes reusability and “pluggability” of components, less code, low coupling, rapid development, and the principle of don’t repeat yourself. Python is used throughout, even for settings files and data models. Django also provides an optional administrative create, read, update and delete interface that is generated dynamically through introspection and configured via admin models.

Install Python

Go to https://www.python.org/downloads/ and download the python installer.

This tutorial uses Django 2.2, and in order to install it, we need to use Python 3.

Follow the instructions and remember to Add Python to PATH

Create a New Django Project

There are a lot of IDEs that you can choose for building a Python project. In this tutorial, we’ll use PyCharm. If you have a student email, you should be able to get a copy for free.

If you decide to use PyCharm, you can create a Django project directly. Make sure that the “Base interpreter” is the python.exe we just installed.

PyCharm will create a virtual environment for the project. A virtual environment makes sure that every Python project uses an independent environment that will not interfere with each other.

It would take a few minutes for PyCharm to setup our project. After everything is done, this is what you should get:

Now, we can start the development server to test if everything works. Open the terminal, which is located at the bottom left corner if you are using PyCharm. It should automatically go to the project root folder and activate the virtual environment. If you are not using PyCharm, you might have to do this manually. Run the following command:

python manage.py runserver

Open the browser and go to http://127.0.0.1:8000/

Django allows you to create multiple apps in a single project. For example, there could be a “blog” app, a “gallery” app, and a “forum” app inside one single project. These apps could share the same static files, images, videos… or they could be completely independent of each other. Depends on your own need.

In this tutorial, we’ll create just one “blog” app. Go back to the terminal, and type in:

python manage.py startapp blog

You should see a new blog folder created in the project directory.

Just like Laravel, Django also uses the MVC structure. However, the terminologies are a bit different. We’ll talk about this in the next post.

Next Post: Django Tutorial #2: URLs, Models, Views and Templates

Tags:

Leave a Reply

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