Installing Django and setting up the server

In order to follow this post we should take a look at http://dan.saigon.ro/how-to-set-up-a-virtual-python-development-environment-with-venv/.
We have our Python virtual development environment now. Let’s do something with it! I called it MyWebsite with a reason. I want to build a Python powered website using Django as a framework.

Installing Django

Before installing Django, you have to cd into the project folder first and activate your development environment.

ddn_ro@Linux:~$ ls
Calibre Library                        examples.desktop  py.html~
data-types-numbers-number-types.html~  Music             PythonProjects
Desktop                                Pictures          Templates
Documents                              Public            Videos
Downloads                              __pycache__
ddn_ro@Linux:~$ cd PythonProjects
ddn_ro@Linux:~/PythonProjects$ ls
MyWebsite
ddn_ro@Linux:~/PythonProjects$ source MyWebsite/bin/activate
(MyWebsite) ddn_ro@Linux:~/PythonProjects$

We have pip so we can install Django using pip by running the following command in the terminal:

pip install Django

The terminal output will be something like shown below:

(MyWebsite) ddn_ro@Linux:~/PythonProjects$ pip install Django
Collecting Django
  Downloading Django-1.11.7-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 204kB/s 
Collecting pytz (from Django)
  Using cached pytz-2017.3-py2.py3-none-any.whl
Installing collected packages: pytz, Django
Successfully installed Django-1.11.7 pytz-2017.3

Start the Django project

We have Django installed now. Next, we have to create our project folder, but before that we have to do the following step:

cd MyWebsite

We will start the Django project within the MyWebsite folder.

We create the Django project by running the following command in the terminal:

django-admin startproject mywebsite

Start Django project

You can replace mywebsite with whatever name you like. Now, within the folder MyWebsite you should have a folder named mywebsite and within mywebsite folder you should have another folder named also mywebsite.

Django project

The first mywebsite folder is just a container but the second mywebsite folder is already in the settings and it is the main hub of the website. You also have a file named manage.py and within the mywebsite folder you should have four files:

  • __init__.py
  • settings.py
  • urls.py
  • wsgi.py

Django project folder

The __init__.py file tells Python that this should be treated as a package. The file is completely blank now. We won’t even touch it.

The settings.py file is the main hub. We will be using it quite a bit. There are a few very important parts within the file. One of them is the INSTALLED_APPS. See it below:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

If you install a new application you have to add it here, manually.
Then, another important thing you have in the settings.py file is the SECRET_KEY. You shouldn’t share it with anyone.
We will go through the other things within this file as we need them. For now, this is all about settings.py.

The urls.py is your controller of the website. It contains url patterns which are regular expressions. You should be confident using regular expressions in order to understand these url patterns.

At this point we have just one url pattern but we are going to add more. The one we have can be seen below:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

The r means what follows is a regular expression.
The ^ marks the beginning of a string.

This file will point to your apps. We will talk more about the urlpatterns later on.

Run the server

Now we are going to interact with the manage.py file. In the terminal, I am currently into the MyWebsite directory, so I am going to switch to the mywebsite directory (the same directory where manage.py file is).

We are going to start the server now and check if everything is installed and set properly. We do that by running the following command:

python manage.py runserver

You will probably get an error related to “migrations”. Don’t care about that! Open your browser and visit http://127.0.0.1:8000 or http://localhost:8000. If you get the message “It worked! Congratulations on your first Django-powered page.” then it means that everything is set up and installed correctly.

Django powered page

So, at this point we have our development server set up and running.

Leave a Reply