preface
Today I’ll start a new series of tutorials on the basics of Django. This is a complete how-to guide to getting started with Django. The tutorial material will be divided into seven parts. We’ll explore all the basic concepts in detail, from installation, preparation of the development environment, models, views, templates, urls, to more advanced topics such as migration, testing, and deployment.
I want to do something different. An easy to learn, informative and fun tutorial. My idea is to intersperse the article with some comics to illustrate the corresponding concepts and scenarios. I hope you enjoy reading this!
But before we get started…
When I was a substitute professor at a university, I used to teach web development to incoming students in the computer science department. At that time, I would always start a new class with the following Confucius quote:
(Translator’s note: I’m not sure it was Confucius, but it was mentioned in ancient China as early as Xunzi’s Confucian Xiaoxi: “If you don’t smell, you don’t smell; if you smell, you don’t see; if you see, you don’t know; if you know, you don’t do; Learn and do. “)
So please do it! Don’t just read the tutorial. Let’s practice! You will gain more with practice and practice.
Why learn Django?
Django is a Web framework written in Python. Web framework is a kind of software that can develop dynamic websites, various applications and services based on Web framework. It provides a set of tools and features that address many common problems associated with Web development, such as: security features, database access, sessions, template handling, URL routing, internationalization, localization, and more.
Using Web frameworks such as Django allows us to quickly develop secure and reliable Web applications in a standardized way without having to reinvent the wheel.
So, what’s so special about Django? For starters, it’s a Python Web framework, which means you can benefit from a variety of open source library packages. The Python Package Repository (PYPI) has over 116,000 packages (as of September 6, 2017). If you want to solve a particular problem, someone has probably implemented a library for it that you can use.
Django is one of the most popular Web frameworks written in Python. It is definitely the most complete, offering a wide variety of out-of-the-box features such as standalone Web servers for development and testing, caching, middleware systems, ORM, template engines, forms processing, and tool interfaces based on Python unit testing. Django also comes with internal batteries and built-in applications, such as an authentication system, a background admin interface for CRUD operations that automatically generates pages, RSS/Atom feeds, sitemaps, and more. There is even a geographic Information System (GIS) framework built into Django.
Django’s development was supported by the Django Software Foundation and sponsored by companies such as JetBrains and Instagram. Django has been around for quite a while now. To date, active projects have been under development for more than 12 years, which also proves that it is a mature, reliable and secure network framework.
Who uses Django?
It’s good to know who uses Django, and to think about what you can do with it. Some of the major sites that use Django are Instagram, Disqus, Mozilla, Bitbucket, Last.fm, and National Geographic.
For more examples, check out the Django Sites database, which provides a list of more than 5,000 Django-powered Sites.
By the way, last year at Django 2016, Instagram employee Carl Meyer, one of Django’s core developers, shared how Instagram is using Django on a large scale and how it supports their user growth. This is an hour lecture, and if you’re interested in learning more, it’s a very interesting lecture.
The installation
The first thing we need to do is install some programs on our computers so we can start using Django. The basic setup includes installing Python, Virtualenv, and Django.
Using virtual environments is not mandatory, but I strongly recommend it. If you are a beginner, it is best to get a good start.
When you are developing a Web site or Web project using Django, it is quite common to have to install external libraries to support development. With virtual environments, each project you develop will have its own environment. This way, dependencies between packages do not conflict. It also allows you to run projects on your local machine on different versions of Django.
You’ll see how easy it is to use it later!
Install Python 3.6.2
The first thing we want to do is install the latest version of Python, which is Python 3.6.2. At least as I write this tutorial. If a newer version is available, use the new version. The next steps should be much the same.
We’ll use Python 3 because most of the major Python libraries have been ported to Python 3, and Python 2 will no longer be supported by the next major django release (2.x). So Python 3 is the right choice.
The best way to do this is through Homebrew installation. If you don’t have Homebrew installed on your Mac, run the following command from your terminal:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code
If you don’t have the Command Line Tools installed, Homebrew may take a little longer to install. But it will help you take care of everything, so don’t worry. Just sit back and wait until the installation is complete.
The installation is complete when you see the following message:
==> Installation successful!
==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics.html
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh
Copy the code
Execute the following command to install Python 3:
brew install python3
Copy the code
Since Python 2 was originally installed on macOS, you will be able to use both versions after installing Python 3.
To run Python 2, run the Python command from the terminal. If you want to run Python 3, use Python3 to start.
We can test it on the terminal:
Python3 - version Python 3.6.2Copy the code
Great, Python is up and running. Next step: Virtual environments!
Install Virtualenv
In the next step, we will install Virtualenv through PIP, a tool for managing and installing Python packages.
Note that Homebrew already has PIP installed for you, named PIp3 under Python 3.6.2.
On the terminal, run the following command:
sudo pip3 install virtualenv
Copy the code
So far, the installations we’ve performed have all been running in an operating system environment. From now on, everything we install, including Django itself, will be installed in the virtual environment.
Think of it this way: For every Django project you start, you first create a virtual environment for it. It’s like having a sandbox for every Django project. So you can run around, install packages, uninstall packages without breaking anything.
I have a habit of creating a folder called Development on my computer. I then use it to organize all my projects and websites. But you can also follow these steps to create your own directory.
Usually, I start by creating a new folder with the project name in the Development folder. Since this will be our first project, we didn’t have to pick a unique name. Now, we can call it MyProject.
mkdir myproject
cd myproject
Copy the code
This folder is the higher-level directory that will store all the files and stuff related to our Django project, including its virtual environment.
So let’s start creating our first virtual environment and installing Django.
In the MyProject folder:
virtualenv venv -p python3
Copy the code
So our virtual environment is created. Before we can start using it, we need to activate the environment:
source venv/bin/activate
Copy the code
If you see (venv) at the front of the command line, the activation is successful, like this:
Let’s try to understand what’s going on here. We created a special folder called venv. This folder contains a copy of Python. After we activate the Venv environment, when we run the Python command, it will use the local copy we stored in Venv instead of the one we installed on the operating system.
Another important thing is that the PIP program is already installed, and when we use it to install Python packages such as Django, it will be installed in the Venv environment.
Note that when we enable venv, we will use the command python (not python3) to call Python 3.6.2, and only PIP (not PIp3) will be used to install the package.
Incidentally, to exit the VENv environment, run the following command:
deactivate
Copy the code
However, let’s keep active for now for the next step.
Install Django 1.11.4
Easy. Now that we’ve started Venv, run the following command to install Django:
PIP install django = = 1.11.4Django has been upgraded to version 2.x, so you must specify a version number to install it in order to keep up with the tutorials
Django 2.x is not recommended unless you have the ability to debug
Copy the code
Now everything is ready!
This article is a translation project of Django’s Getting Started guide, sponsored by the public account “Zen of Python”. Follow the public account to obtain the following chapters
Translator: vimiix
The original address: simpleisbetterthancomplex.com/series/2017…