Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

Django Development Environment

In the last tutorial, we have seen what is Django and its architecture. In this tutorial, we will start working on Django by setting up the environment on the local machine and then we will create our first project. Then, we take a look at how Django works in practice.

Topics to be covered

We will cover the following topics in this course

  • Software to be installed
  • How to create a new Virtual Environment for our project
  • How to install Django in the virtual environment
  • How to create our first project
  • How to start the built-in server for our project
  • Summary

Software to be installed

We will need to download and install the following programs.

Python 3

Django is based on Python language. We will work with the latest version of Python which is Python 3. You can download the setup for Python from the official website. Python is crossplatform so you can select the version for your operating system. Python installers are available for Windows, MacOS and Linux.

Python pip

If you know python, you know how libraries and modules are important. Django is the same. Each Django project has lots of dependencies.

How can we add them to our project easily?

How about when we need to update the packages?

Where should I find a package?

The reason is a program called a package manager. You do not copy-paste a new package to your project. Instead, you use a package manager to handle the work. Pip is a python package manager.

How to install pip on Ubuntu

Run the following command

sudo apt install python3-pip

Usually, the version of pip you have installed using the apt is not always the latest. Since pip is a python software itself, you can update it using itself. To get the latest version just run:

sudo pip install --upgrade pip

How to install pip on Windows

Download the installer script from here:

[https://bootstrap.pypa.io/get-pip.py]{.underline}

And run it using python go to your command prompt and run the file. For example if the file is downloaded to C:\Users\'Username'\Downloads run:

python "C:\Users\'Username'\Downloads\get-pip.py"

HERE,

  • 'Username' is the username of your windows profile. For example, if your user name is Rodrick Kazembe, then you have to run the command python "C:\Users\Rodrick Kazembe\Downloads\get-pip.py"

Then add it to your environment variables:

go to control panel > System and Security > System

Select Advanced system settings. In the pop-up page, select Environment Variables

Select new and enter the path to you python script, for example if you have installed python 3.5 in C:\python35 will be:

C:\Python35\Scripts

On the new menu, click OK. No, you should be able to use pip command in your shell.

How to install pip on OSX

Download the installer script:

[https://bootstrap.pypa.io/get-pip.py]{.underline}

Then, run it with python:

python3 get-pip.py

Virtual Environment

As a developer, you will have several Django projects on your system. Each would have lots of dependencies., with different python and Django versions. How can we handle this?

We do not put all python packages in our system packages. We also do not use our system python.

We create a virtual environment for each project with a specific python version and put all the dependencies in that virtual env.

How to install virtualenv on ubuntu

To Install virtualenv on ubuntu, run the following command

sudo pip install --upgrade virtualenv

How to install virtualenv on Windows 10

To Install it on Windows10 enter:

pip -m install virtualenv

How to install virtualenv on Mac OS

To Install virtualenv on OSX, enter:

sudo pip install virtualenv

Creating a new environment:

We create a new directory and cd into it:

mkdir tutorial && cd tutorial

Now we create a new virtual environment

virtualenv venv

We have a new directory venv

take a look at it:

ls venv

but we have not used it yet. Look at your python version:

python --version

It is the default version of your system. You can enable the virtual environment in ubuntu or Mac OSX by:

source venv/bin/activate

To enable the virtual environment on Windows 10 enter the path of activate file:

\path\to\env\Scripts\activate

for example if you have created venv on C:\Users\'Username\'\venv enter:

C:\Users\'Username\'\venv\Scripts\activate.bat

Now check the version of python again:

python --version

As you see, the version has been changed. It means that you are in a new virtual environment.

Install Django

We install Django in our new virtual environment as a new package:

pip install django

Also we need mysqlclient package to connect django to mysql, so:

Installing pre-requisites

Ubuntu

sudo apt-get install python-dev default-libmysqlclient-dev

RedHat / Centos

sudo yum install python-devel mysql-devel

MacOS

sudo yum install python-devel mysql-devel

Finally, we install mysqlclient like so

pip install mysqlclient

Starting A new Django Project:

We have not started our project yet. To start the project use this new command:

django-admin startproject projectname

We define project name as bookkeeper:

django-admin startproject bookkeeper

As you see we have a new folder here. It contains the code of our project. What is in it?

-We have a manage.py file here. We can do some operations like running a local server or changing our database by executing this file.

-A new folder named equal to our project name. Let's check its files:

We have two files that we will work on:

  • settings.py: contains the settings in the project. The variables in the file seem to be self-explanatory. We will explain them when needed in the tutorial.
  • urls.py: This file is the root url file. When you enter a URL in the application, it checks its patterns and calls the related method (in the views). We will check it later

Running the project:

For putting on a server and serving it, you need a web server software like Apache httpd or nginx.

However, it would make development difficult if you always need to run the application using a web server even on your local machine. The solution?

python lets us serve it on our local machine without any other software. This is just for development, and it is not suitable for deployment.

It allocates a port on the localhost for our application. So if you type 127.0.0.1:[port_number] on your browser you will see our application served.

Now we use our first command of manage.py:

python manage.py runserver

The address on our served application is written in the shell. Enter it in your browser and you will see the home page of application.

MySQL Server

MySQL is one of the most commonly used database engine when developing Django applications. MySQL is a server client database engine that you can download for free. It is crossplatform and works on Windows, MacOS, and Linux. You can download the setup from the official website.

How to Install MySQL on Ubuntu

Open the terminal then run the following commands

apt-get update
sudo apt-get install mysql-server

HERE,

  • The above command downloads and install MySQL Server.

When the installation is completed, run the following command

sudo mysql_secure_installation

HERE,

  • The above command starts a wizard that will guide you through the process of securing your installation of MySQL.

Finally, you can run the following command to start the server

sudo systemctl status MySQL.service

Let's now install MySQL Workbench. It is a MySQL management tool that is used to manage databases. You can create and delete databases, create tables, views, function etc via MySQL Workbench.

Run the following command to install MySQL Workbench

sudo apt-get install mysql-workbench

How to Install MySQL on Windows

To install MySQL on Windows 10,

Download MySQL installer for windows from here: [https://dev.mysql.com/downloads/windows/installer/8.0.html]{.underline}

Then, run the installer file. In the "Choose a Setup Type" , choose Developer default .

In Configurations:

For group replication. Choose Standard Mysql server

In Type of networking, choose Developer Computer as config type

set TCP/IP port number to 3306

In authentication method, choose use strong password.

You don't need to create a new user at this moment. Since we are in local machine and in development, we can use the root server. Just set a password for root user.

How to install MySQL on OSX

Download it from and install MySQL using the installer file:

https://dev.mysql.com/downloads/mysql/

If your system is password protected, The installer will ask you for system password. Enter your password and continue.

Installer will generate a password for MySQL root user automatically. Copy and save it in a text file, because you will need it in the future.

Download and Install MySQL workbench:

https://dev.mysql.com/downloads/workbench/

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

Django Hello World

Leave A Reply