[Django Series] – Setting up working environment for a Django Project

Python is easy to learn but difficult to master. Saying so, I mean the introductory course for this language, as well as its framework should be utmost simple. In this series, I will write about the process of building up a Django website in the most cut and dry words that you could be more than comfortable to understand.

To follow this series, I assume you have some knowledge in HTML,CSS and well as Python core since all the basic things in Django will be built from these.

Now, let’s dive straight into the point. To create a dynamic website powered by Python and Django, we need two things: Python and Django and other things related to two big bosses (sounds simple enough, huh?).  Well, only 6 steps (and one optional) are needed:

1. Install Python

Although I have assumed that you at least have some basic knowledge in Python, I still need to include this steep. Why? Well, you might be working on a 2.x Python version and it is not good at all since it will be no longer supported in 2020. Plus, Python 3.x has some huge changes and only Python 3.x can work along with Django 2.x. So let’s go here and download the newest Python version. At this point of time, it is 3.7.2.

Note: If you’re on a Windows machine, don’t forget to tick on ‘Add Python 3.7 to PATH’ when the instalment window pops up.

2. Install pip

Pip is actually a recursive acronym for “Pip Install Packages”. Basically, Python itself has some very awesome packages that you can work with. But, the more amazing packages are not included (Django is a typical example) and you must have a special tool to install them. The special tool is pip. To install pip, go to your terminal (on Mac or Linux) or Shell (on Windows) and type:
python3 -m install -u pip

Note: On Windows machines, you only need to type python (instead of python3) since Mac OS has pre-installed python with python 2. So we need to type python3 to differentiate with it.

3. Install Virtual Environment

It sounds scary, doesn’t it? Not at all. Virtual Environment is just a FOLDER. So why do we need that?

Imagine you work on a project with Python 2.x and Django 1.x (at this time we use Django 2.x). After a period of time, you accidentally modify some dependencies and environment variables such as updating to Python 3.x and Django 2.x. And Boooom, your project won’t work anymore since the new version doesn’t recognise the old syntax. See the trouble? That’s why you need a Virtual Environment FOLDER (from now on we’ll call it Virtual Environment only) to WRAP ALL the dependencies and environment variables that your current application needs into a file system separate from the rest of the software on your computer. (It’s like an isolated world and whenever you want to work with your project, go straight to this world). Again, type this command line in your terminal:
pip3 install virtualenv

(Again, only ‘pip’ with Windows machines)

When you see the message ‘Successfully installed …’, it means ‘Done! No need to think about it anymore…’

4. Create a project folder with virtual environment in it

Create a new project folder so it contains all of your projects and, of course, virtual environment. Type this command line to create a new project folder (name it anything you want):
mkdir Django_project

Then access to the Virtual Environment:
virtualenv myenv

‘virtualenv’ is to call the Virtual Environment package to create a virtual enviroment in the folder myenv (it’s ok if you want to name it differently)

To access virtual environment, in the Django_project folder use this command line:
source myenv/bin/activate

(note: if you are on Windows machine, use this instead:
myenv\scripts\activate )

After you see the command prompt has changed to (myenv)…, you know that you’re already in the virtual environment.

Ohh, I almost forgot, to exit a virtual environment, simple type:
deactivate

5. Install Django

Last step, install Django via pip in the Terminal (while you’re still in Virtual Environment):
pip3 install django

In case, you really want to specify a particular version, use:
pip3 install "django=2.1"

(If you expect it installs the newest one, the forget the above)

6. Start a Project

Actually, you’ve fished instalment steps. Now is the time you should mess up with Django. In the Django_project folder, I type this command:
django-admin startproject helloWorld

Done, now cd to the helloWorld folder and connect to the server by this command:
cd helloWorld
python3 manage.py runserver

If it is successful, you’ll see the url to the server as http://127.0.0.1:8000/. Go to that and you will see this screen:

You’re done!!! Congrats!

* * * Choose your text editor (optional)

Feel free to choose any text edit you like or be familiar with. But if you ain’t sure about it, go with Visual Studio code at here.

Leave a Reply

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