14/04/19 15 minutes read 1675 Naren Allam
In order to install Django with pip, we need to install 'python3-pip' package to the ubuntu 18.04 system.
Now that we have pip installed, that means we have the ability to quickly install other necessary packages for a Python environment.
But before installing Django, we also have to install virtualenv.
Now install 'virtualenv' by running the command
Now create a virtual environment with any name (for example, 'venv') using the virtualenv package installed, and also activate the environment by running the command
We will know that we have virtualenv started, when we see that the prompt in our console is prefixed with (venv).
Note:- To come out of the environment, type the command 'deactivate' and press 'Enter' button on the keyboard.
When working within a virtual environment, python will automatically refer to the correct version so we can use python instead of python3.
Now we need to install various development libraries required to build the application. They includes python3-dev for compiling any Python extensions and their supporting standard libraries, as well as the libmysqlclient-dev package, which is required to build the MySQL client in the todo backend application.
This can be done by running the below commands in the terminal
Now we have all important dependencies in place. So, we can finally install Django and all the other tools required to develop django web application.
To install them, run the command
Here we have installed django, pillow and djangorestframework.
Thus we have installed Django and other tools which are needed to get started building Django web applications.
So, we can start creating our first django project.
Django Application Creation
As the installation and setup is completed successfully, we will create our first django project named 'project' and navigate into the project directory by running this command
In the above command, django-admin tool creates the project folder, basic file templates and project management script (manage.py).
Now navigate into 'project' folder and then we can look to see what project files were just created by running the command 'ls' to list the contents of that directory (to see what files were created).
We will notice the output that shows this directory contains a file named manage.py and a folder named project.
The manage.py file is similar to django-admin and puts the project’s package on sys.path. This also sets the DJANGO_SETTINGS_MODULE environment variable to point to our project’s settings.py file.
The manage.py script is used to create applications, work with databases, and start the development web server. It’s a convenience script that allows you to run administrative tasks like runserver , createsuperuser , migrations of your database models.
We can view the manage.py script in our terminal by running the 'less' command like so
When we’re finished reading the script, press 'q' to quit viewing the file.
Now navigate to the 'project' directory to view the other files that were created. To list the contents of the directory, run 'ls' command
We will see four files like this :
After installation, to open the project directory in visual studio code editor, run the command
Now create an application named 'app' for this project. To create app, make sure we’re in the same directory as manage.py and run the command
That’ll create a directory named 'app'. Now navigate to 'app' directory and here we can look to see what app files were just created by running the command 'ls' to list the contents of that directory (to see what files were created). We will notice the output that shows like this
Remove the default database engine that is automatically set for you.
By default, Django adds the SQLite3, we don’t want that. So, remove the definitions from DATABASES in settings.py. It should look like this, after you remove the default key:
Here, we need to verify that the application is working properly or not. We can do this by simply running the server. To run the server, use the command
The command will run django server on port '8000' as shown below
Note:- You will see that you have unapplied migrations in the output. But, don’t worry, this does not affect the initial setup of our application.
So, open the web browser and go to http://localhost:8000/ to check if you see a Welcome page like this
Note 1:- Press 'Ctrl+c' to exit from the django runserver.
With the project and app created, we now need to sync our database for the first time and also create an initial user(superuser/admin) and set a password for that user. Before this we need to install MySQL as we decided to use in our project.
In order to use MySQL with our project, we must install MySQL server. To do so, first deactivate the environment and install mysql-server as shown below.
After successful installation, connect to MySQL server by running the below command.
Now we have to set password for the 'root' user of MySQL. This can done by running the following command
The above command tells MySQL to change the password for 'root' user at 'localhost'.
As a final step following any updates to the user privileges, be sure to save the changes by issuing the FLUSH PRIVILEGES command from the MySQL prompt This tells database server to reload the grant tables
Then to come out of the shell use EXIT command.
As we set the password for the 'root' user, now login as root user by the command given below.
Note: When you input the password, it will not be visible.
Now create a new user by issuing the command
Now we have to grant all privileges to the 'user' with 'grant option'. This can be done by running the command
With GRANT OPTION user can grant other users, permissions to the database.
To come out of the MySQL shell, type the command
Then login as user for checking, whether the user is created or not.
Note: When you input the password, it will not be visible.
After login, create any database named 'webnews' for our project which we will you later. To create a database in MySQL, run the command
To check whether the database is created or not, run the command
Then come out of the MySQL Shell by running the EXIT command.
Add the MySQL database connection settings like db_name, username, password etc.., in settings.py file.
Our configuration should look like as shown below
Then save the changes made by pressing 'Ctrl+s'.
Next, we have to configure the Django admin. Django will automatically generate the database for a superuser. Before we create the superuser, create a set of rules by running the command
'makemigrations' generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py).
It does not execute those commands and so tables will not be created after makemigrations.
After applying makemigrations we can see those SQL commands with sqlmigrate which shows all the SQL commands which has been generated by makemigrations.
Now to apply all the migrations, run the command
'migrate' create the table in SQL database using the commands which have been generated by makemigrations.
The migrate command looks at the INSTALLED_APPSsetting of project/settings.py file and creates any necessary database tables according to the database settings in the project/settings.py file.
We’ll see a message for each migration it applies as shown below.
Django automatically creates 10 default tables in MySQL database. To verify this login to mysql database and check as shown below.