Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It's free and open source.
This post covers how to properly start your Django projects. This post is base on these versions of both Django (2.1) and Python (3.6.7). If you are not familiar with the virtual environment, follow the How to create a virtual environment.
By the end of this post, you’ll be able to create and modify new Django projects in just a few keystrokes.
Step 1: Create a Folder where your Django projects will reside.
To do this, Open your terminal and type in the following commands. In this tutorial, we will be creating the folder on the desktop.
#Creating a folder on the desktop for our Django projects :~$ mkdir Desktop/mydjangoprojects
Step 2: Creating a virtual environment for our Django project.
Run the following command to create a virtual environment. Note: Our virtual environment will be created inside mydjangoprojects directory as follows.
#This command will create a virtualenv in mydjangoprojects directory virtualenv -p python3 Desktop/mydjangoprojects/projectvirtenv
Step 3: Activating your virtual environment
Execute the following command in your terminal to activate virtualenv
#This command will activate your virtual environment :~$ source Desktop/mydjangoprojects/projectvirtenv/bin/activate
After activating, your terminal should look like below
#nsa@nsa is the name of my pc yours can be different. (projectvirtenv) nsa@nsa:~$
Step 4: Installing Django in our virtual environment
Run the following command to install Django
# Installing Django pip install Django==2.1
Please do not use sudo in your virtual environment when installing pip dependencies.
Step 5: Creating a Django project.
Before we start our Django project, let's navigate to the directory where our project will be stored (Desktop/mydjangoprojects/) let's call our project djangoproject.
#Navigation to the directory where our project will be created. (projectvirtenv) nsa@nsa:~$ cd Desktop/mydjangoprojects/
After this stage, your terminal should look like this
# After activating and navigating to the directory, your terminal should look like this (projectvirtenv) nsa@nsa:~/Desktop/mydjangoprojects$
Now Creating our project.
Run the following command to create a project called djangoproject.
# Our Django project creating command. django-admin.py startproject djangoproject
Now let’s confirm everything is working by running Django’s local web server.
Navigate into the djangoproject directory-->
# navigating to our project directory cd djangoproject/
Run the command below to start the local server
# starting the local server python manage.py runserver
After that, you should see this on your terminal
Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. April 23, 2019 - 08:45:28 Django version 2.1, using settings 'djangoproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
If you visit http://127.0.0.1:8000/ you should see the following image: