A Complete Beginner's Guide To Django - : Series
A Complete Beginner's Guide To Django - : Series
A Complete Beginner's Guide To Django - : Series
SERIES
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 1/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Introduction
Today I’m starting a new tutorial series about Django fundamentals. It’s a
complete beginner’s guide to start learning Django. The material is divided into
seven parts. We’re going to explore all the basic concepts in great detail, from
installation, preparation of the development environment, models, views,
templates, URLs to more advanced topics such as migrations, testing, and
deployment.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 2/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
So, hands on! Don’t just read the tutorials. Let’s do it together! You will learn
much more by doing and practicing.
Why Django?
Django is a Web framework written in Python. A Web framework is a software
that supports the development of dynamic Web sites, applications, and
services. It provides a set of tools and functionalities that solves many common
problems associated with Web development, such as security features,
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 3/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
So, what’s so special about Django? For starters, it’s a Python Web framework,
which means you can benefit from wide a range of open source libraries out
there. The Python Package Index repository hosts over 116K packages (as per
6 of Sep. 2017). If you need to solve a specific problem, the chances are
someone has already implemented a library for it.
Django is one of the most popular Web frameworks written in Python. 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.
more than 12 years now, proving to be a mature, reliable and secure Web
framework.
It’s good to know who is using Django out there, so to have an idea what you
can do with it. Among the biggest Web sites using Django we have: Instagram,
Disqus, Mozilla, Bitbucket, Last.fm, National Geographic.
For more examples you can see the Django Sites database, they offer a list of
over 5K Django-powered Web sites.
By the way, last year, in the Django Under The Hood 2016 conference, Carl
Meyer, a Django core developer, and Instagram employee, gave a talk on how
Instagram use Django at scale and how it supported their growth. It’s a one
hour talk, but if you are interested in learning more, it was an entertaining talk.
Installation
The first thing we need to do is install some programs on our machine so to be
able to start playing with Django. The basic setup consists of installing Python,
Virtualenv, and Django.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 5/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
When developing Web sites or Web projects with Django, it’s very common to
have to install external libraries to support the development. Using virtual
environments, each project you develop will have its isolated environment. So
the dependencies won’t clash. It also allows you to maintain in your local
machine projects that run on different Django versions.
The first thing we want to do is install the latest Python distribution, which is
Python 3.6.2. At least it was, by the time I was writing this tutorial. If there’s a
newer version out there, go with it. The next steps should remain more or less
the same.
We are going to use Python 3 because the most important Python libraries
have already been ported to Python 3 and also the next major Django version
(2.x) won’t support Python 2 anymore. So Python 3 is the way to go.
The best way to go is with Homebrew. If you don’t have it installed on your
Mac yet, run the following command in the Terminal:
If you don’t have the Command Line Tools installed, the Homebrew
installation might take a little bit longer. But it will take care of everything for
you, so no worries. Just sit back and wait until the installation completes.
You will know when the installation completes when you see the following
message:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 7/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Since macOS already ships with Python 2 installed, after you install Python 3,
you will have both versions available.
To run Python 2, use the python command in the Terminal. For Python 3, use
python3 instead.
python3 --version
Python 3.6.2
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 8/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Installing Virtualenv
For the next step, we are going to use pip, a tool to manage and install Python
packages, to install virtualenv.
Note that Homebrew already installed pip for you under the name pip3 for
your Python 3.6.2 installation.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 9/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
So far the installations that we performed was system-wide. From now on,
everything we install, including Django itself, will be installed inside a Virtual
Environment.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 10/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Think of it like this: for each Django project you start, you will first create a
Virtual Environment for it. It’s like having a sandbox for each Django project. So
you can play around, install packages, uninstall packages without breaking
anything.
Usually, I start by creating a new folder with the project name inside my
Development folder. Since this is going to be our very first project, we don’t
need to pick a fancy name or anything. For now, we can call it myproject.
mkdir myproject
cd myproject
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 11/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
This folder is the higher level directory that will store all the files and things
related to our Django project, including its virtual environment.
So let’s start by creating our very first virtual environment and installing Django.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 12/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Our virtual environment is created. Now before we start using it, we need to
activate:
source venv/bin/activate
You will know it worked if you see (venv) in front of the command line, like this:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 13/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Another important thing is that the pip program is already installed as well, and
when we use it to install a Python package, like Django, it will be installed
inside the venv environment.
Note that when we have the venv activated, we will use the command
python (instead of python3 ) to refer to Python 3.6.2, and just pip
(instead of pip3 ) to install packages.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 14/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
deactivate
It’s very straightforward. Now that we have the venv activated, run the
following command to install Django:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 15/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
After we run the command above, it will generate the base folder structure for a
Django project.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 16/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
__init__.py: this empty file tells Python that this folder is a Python package.
settings.py: this file contains all the project’s configuration. We will refer to
this file all the time!
urls.py: this file is responsible for mapping the routes and paths in our
project. For example, if you want to show something in the URL /about/ ,
you have to map it here first.
wsgi.py: this file is a simple gateway interface used for deployment. You
don’t have to bother about it. Just let it be for now.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 17/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Django comes with a simple web server installed. It’s very convenient during
the development, so we don’t have to install anything else to run the project
locally. We can test it by executing the command:
For now, you can ignore the migration errors; we will get to that later.
Now open the following URL in a Web browser: http://127.0.0.1:8000 and you
should see the following page:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 18/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Django Apps
In the Django philosophy we have two important concepts:
It’s important to note that you can’t run a Django app without a project. Simple
websites like a blog can be written entirely inside a single app, which could be
named blog or weblog for example.
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 19/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
It’s a way to organize the source code. In the beginning, it’s not very trivial to
determine what is an app or what is not. How to organize the code and so on.
But don’t worry much about that right now! Let’s first get comfortable with
Django’s API and the fundamentals.
Alright! So, to illustrate let’s create a simple Web Forum or Discussion Board.
To create our first app, go to the directory where the manage.py file is and
executes the following command:
myproject/
|-- myproject/
| |-- boards/ <-- our new django app!
| | |-- migrations/
| | | +-- __init__.py
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- tests.py
| | +-- views.py
| |-- myproject/
| | |-- __init__.py
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 20/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
| | |-- settings.py
| | |-- urls.py
| | |-- wsgi.py
| +-- manage.py
+-- venv/
migrations/: here Django store some files to keep track of the changes you
create in the models.py file, so to keep the database and the models.py
synchronized.
admin.py: this is a configuration file for a built-in Django app called Django
Admin.
models.py: here is where we define the entities of our Web application. The
models are translated automatically by Django into database tables.
tests.py: this file is used to write unit tests for the app.
views.py: this is the file where we handle the request/response cycle of our
Web application.
Now that we created our first app, let’s configure our project to use it.
To do that, open the settings.py and try to find the INSTALLED_APPS variable:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 21/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
As you can see, Django already come with 6 built-in apps installed. They offer
common functionalities that most Web applications need, like authentication,
sessions, static files management (images, javascripts, css, etc.) and so on.
We will explore those apps as we progress in this tutorial series. But for now,
let them be and just add our boards app to the list of INSTALLED_APPS :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boards',
]
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 22/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Using the analogy of the square and circles from the previous comic, the yellow
circle would be our boards app, and the django.contrib.admin,
django.contrib.auth, etc, would be the red circles.
Hello, World!
Let’s write our first view. We will explore it in great detail in the next tutorial.
But for now, let’s just experiment how it looks like to create a new page with
Django.
Open the views.py file inside the boards app, and add the following code:
views.py
def home(request):
return HttpResponse('Hello, World!')
Views are Python functions that receive an HttpRequest object and returns
an HttpResponse object. Receive a request as a parameter and returns a
response as a result. That’s the flow you have to keep in mind!
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 23/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
So, here we defined a simple view called home which simply returns a
message saying Hello, World!.
Now we have to tell Django when to serve this view. It’s done inside the urls.py
file:
urls.py
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^admin/', admin.site.urls),
]
If you compare the snippet above with your urls.py file, you will notice I added
the following new line: url(r'^$', views.home, name='home') and
imported the views module from our app boards using
from boards import views .
As I mentioned before, we will explore those concepts in great detail later on.
But for now, Django works with regex to match the requested URL. For our
home view, I’m using the ^$ regex, which will match an empty path, which is
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 24/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 25/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
Conclusions
That was the first part of this tutorial series. In this tutorial, we learned how to
install the latest Python version and how to setup the development
environment. We also had an introduction to virtual environments and started
our very first Django project and already created our initial app.
I hope you enjoyed the first part! The second part is coming out next week, on
Sep 11, 2017. It’s going to be about models, views, templates, and URLs. We
will explore together all the Django fundamentals! If you would like to get
notified when the second part is out, you can subscribe to our mailing list.
Just so we can stay on the same page, I made the source code available on
GitHub. The current state of the project can be found under the release tag
v0.1-lw. The link below will take you to the right place:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 26/27
9/8/2019 A Complete Beginner's Guide to Django - Part 1
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1.html 27/27