Installing Django and creating your first project

 

If you have already installed Django, you can skip and jump directly to Creating your first project.

If you haven’t installed Django yet, here is a quick guide to installing Django for local development.

Django works well with Python versions 2.7 or 3.If you are not sure if Python is installed in your computer, you can verify it by typing python in the terminal. If you see something like the following, then Python is installed in your computer:

If you are using Linux and you will just type python then terminal will display the python version 2.7 but if you have installed the latest version of python Type python3 and it will display the latest version of python on your system.

Make sure when you are running your program or server you are using python3 in the terminal.

 

 

Since you are going to use Python 3, you don’t have to install a database. This Python version comes with the SQLite database built-in. SQLite is a lightweight database that you can use with Django for development.

Creating an isolated Python environment

It is recommended that you use virtualenv to create isolated Python environments, so you can use different package versions for different projects, which is far more practical than installing Python packages system wide. Another advantage of using virtualenv is that you won’t need any administration privileges to install Python packages. Run the following command in your shell to install virtualenv:

pip3 install virtualenv

After you install virtualenv, create an isolated environment with the following command:

virtualenv my_env

This will create a my_env/ directory including your Python environment. Any Python libraries you install while your virtual environment is active will go into the my_env/lib/python3.7/site-packages directory.

Run the following command to activate your virtual environment:

source my_env/bin/activate

The shell prompt will include the name of the active virtual environment enclosed in parentheses, like this:

(my_env) [[email protected] django]$

You can deactivate your environment anytime with the deactivate command.

 

 

Installing Django with pip

pip is the preferred method for installing Django. Run the following command at the shell prompt to install Django with pip:

Installing Django with pip

pip is the preferred method for installing Django. Run the following command at the shell prompt to install Django with pip:

pip3 install Django==2.2 (Make sure you are using pip3) (You can find more information here https://www.djangoproject.com/download/)

 

 

Now check if Django has been successfully installed. Run python3 on a terminal and import Django to check its version:

 

 

Creating your first project

Django provides a command that allows you to easily create an initial project file structure. Run the following command from your shell:

django-admin startproject mysite

This will create a Django project with the name mysite.

 

 

manage.py :  A command-line utility to interact with your project. It is a thin wrapper around the django-admin.py tool. You don’t need to edit this file.

mysite/ : Your project directory consist of the following files:

     ° ° __init__.py : An empty file that tells Python to treat the mysite

directory as a Python module.

     ° ° settings.py : Settings and configuration for your project. Contains initial default settings.

    ° ° urls.py : The place where your URL patterns live. Each URL defined here is mapped to a view.

° ° wsgi.py : Configuration to run your project as a WSGI application.

° ° urls.py : The place where your URL patterns live. Each URL defined here is mapped to a view.

° ° wsgi.py : Configuration to run your project as a WSGI application.

The generated settings.py file includes a basic configuration to use an SQLite database and a list of Django applications that are added to your project by default. We need to create the tables in the database for the initial applications.

Open the shell and run the following commands:

cd mysite

python3 manage.py migrate

 

 

Running the development server

Django comes with a lightweight web server to run your code quickly, without needing to spend time configuring a production server.

Start the development server by typing the following command:

python3 manage.py runserver

 

 

Copy the link http://127.0.0.1:8000/ and paste in your favorite browser.

 

 

 

By Amit Pratap Singh

Leave a Reply