Make a simple Django 3.2 Portfolio Project as Data Scientist — Part 1
Introduction
In this blog, I am going to develop my own portfolio website using Django. It will be few part series. As I am a data science student I need to learn every day and make my portfolio visible to everyone to know what project I am doing now and what I am capable of.
- Make a simple Django Portfolio Project as Data Scientist — Part 1
- Make a simple Django Portfolio Project as Data Scientist — Part 2
- Make a simple Django Portfolio Project as Data Scientist — Part 3(Final)
Table of Contents
- Introduction
- How Django Works
- Installation Phase: Python, pip, MYSQL, virtualenv.
- How to clone and work with git
- Connect our database with Cpanel/Hostgator
- Run Django welcome page.
Project Link: https://github.com/tusher16/My-portfolio-project.git
Project website Link: https://www.tusher16.com
In this first part, I will cover the prerequisite installations and start the Django web server locally. And will do my working environment in Linux both on chrome os and ubuntu 20.04 LTS.
As a first step I have already created my resume, you can see how I have created a simple resume in google docs. this resume template you can also use. In few weeks I will make how you can also make a simple resume.
Why Django? Django is an open-source web framework by Python itself. For my everyday data science and machine learning process, I have to know Python. Python is a high-level language as Django is also a high-level framework.
As I am already learning python and this same language it’s easier for me to drive in to use Django as my first portfolio website. I can use flask as it’s also in python and uses a micro framework method. I chose Django over flask because all the things I need for this project are built into the framework for example Templates, Admin, and ORM. And major databases are also built right into Django.
Let us know how Django works. Django is an MTV(MODEL TEMPLATE VIEW) framework, instead of the MVC(Model View Compose) framework. Where Model is the same, Template works as View and View as Controller.
For more, you can see this medium blog to Understand Django’s WorkFlow.
In fig 1.3 we can see how Django interacts with each other in order to work in the background.
Let’s drive into making the website. I am using the Linux environment, we can use the same commands in Windows Microsoft Powershell, and few things are basically the same, but some software and library things are a little different in Windows, so I recommend doing it on a Linux system.
Check python:
See what python version we are using:
python --version
See what’s your python version, make sure you have 3.6 and upper versions, you may have 2.7 also but for Django we will be using python 3.x version.
If you don’t have python it will show like this below, I have typed 3.8 for example.
If you don’t have python, just go to this link. And follow the installation process for UBUNTU.
Check pip:
sudo apt-get install python3-pip
If you don’t have pip it will show like this:
Let's install pip:
sudo apt-get install python3-pip
If pip is installed then I will show this:
pip is a package management system written in Python used to install and manage software packages. We need pip for installing required packages. For example, we need a virtual environment for running Django.
Install virtualenv:
Before installing Django, it’s recommended to install Virtualenv that creates new isolated environments to isolate your Python files on a per-project basis. This will ensure that any changes made to your website won’t affect other websites you’re developing. (Reference)
Then install virtualenv using pip3:
sudo pip3 install virtualenv
After install, you can check your version like this.
In this project, we will also use git for version control, and for the database, I am using MySQL. Django comes with SQLite but my hosting server MySQL is lots easier to configure. But I am sticking to MySQL.
Install MySQL:
Let's install MySQL-server on my Linux.
sudo apt-get updatesudo apt-get install mysql-server
In chrome os, I followed this process.
Major things are installed to start our project folder. Let's create a folder:
mkdir portfolio-project
Now cd into the project folder, and on the other side let's create a repository on the GitHub website and clone it to this new portfolio-project.
GIT:
git clone https://github.com/tusher16/My-portfolio-project.git
After configuring git we have successfully cloned our repository.
Try git status to check if everything is working fine or not.
Now let's create a virtual environment:
virtualenv myenv
you can use any name instead of myenv.
After we will get this output:
For activating the virtual environment we have to write:
source venv/bin/activate
To deactivate:
deactivate
We won’t deactivate now, because we will do all our installation libraries and work inside this virtual environment.
After activation, we will see (myenv) before a username like this.
I miss spelled pip freeze, it will be pip freeze, this command will show what library and packages are installed in the environment. As mine is a new virtual environment it’s given me none.
Now let’s install Django into our fresh environment.
(myenv) $ pip install Django
After successful install you will get this:
If we check now the pip freeze command we will get this:
These libraries are installed along with Django 3.2.4. if someone tries to learn and use this full blog I recommend using the same version of Django.
Create a Django Project:
(myenv)$ django-admin startproject My-portfolio-project
We will get this new directory portfolio. And the directory structure should look something like this:
My-portfolio-project/
│├── portfolio/│ ├── portfolio/│ │ ├── __init__.py│ │ ├── settings.py│ │ ├── urls.py│ │ └── wsgi.py│ ││ └── manage.py│└── myenv/
For a more clean directory let’s move and remove some folders.
(myenv)$ mv portfolio/manage.py ./(myenv)$ mv portfolio/portfolio/* portfolio(myenv)$ rm -r portfolio/portfolio/
After removing the files we will get this new structure:
My-portfolio-project/
│├── portfolio/│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py│├── venv/│└── manage.py
Now as our file structure is ready, now start the server with this command:
(myenv)$ python manage.py runserver
We will get this output in the terminal:
After going to http://127.0.0.1:8000/ we will get the “The install worked successfully” page. This time I am ignoring the database migration process because we didn’t set up our database in the settings.py file. We will migrate our database after setup the MySQL connection.
Our Django website is finally running.
For using this project in any other Linux environment we will use the “ pip freeze requirements.txt” command, which will create a list of libraries that are installed in this virtual environment. So that we can use this requirements.txt file to install the same version of Django and other libraries with no issue.
pip3 freeze > requirements.txt # Python3pip freeze > requirements.txt # Python2
If we want to see what’s inside the requirement.txt file just “ nano requirements.txt “
Now let’s upload all the files to our git repository. check the git status to what it shows, it will ask to add all the files and then commit it. After success, full commit. We can push our files into the main branch. And from there we can clone it using a branch anyone can modify from other users.
git add .git commit -agit push origin main
Anyone can skip this MySql part if someone is not comfortable, just skip this part and go to the direct migration section. blew code Is the SQLite config.
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
}
MySQL Config:
For my MySQL database connection, I followed this website. As our portfolio website generated the skeleton project, we now have a settings.py file.
In the setting.py file we have changed our timezone:
In the beginning, just import os
Import os
And in the bottom, we have added blew lines:
STATIC_URL = ‘/static/’STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
Now we will install mysqlclient, for this, we have to install a few libraries first. I am following this link: https://pypi.org/project/mysqlclient/
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
Then we can install mysqlclient via pip now:
pip install mysqlclient
We can use a local SQL server but for this project, I am using my hosting provider server’s existing MySQL database. With this remote MySQL connection I can modify and use the project from anywhere. This has another benefit for example After finishing the project I can just deploy the Django project file only the database server stays the same.
Now as mysqlclient is installed. Now let’s connect our database.
First, I have created a database server from Cpanel, by going ‘MySQL® Databases’ section, from the ‘Create New Database’ section I have created my database then from ‘MySQL Users’ Section, I created a new username and password and granted full access to my new database.
I have to also add my host’s IP address in order to remote access the database. At first, the connection was not working so I figured out that the port of my hostings server which 3306 is blocked by the hosting provider. So then I contacted them through service ticket then they opened the 3306 port for me.
This is from my database section:
This is from PHPMySQL page:
Now that I have successfully created my database, let’s connect it through settings.py in Django.
Let’s put my credentials into the Database section:
Database Migration:
Now let's run our migration:
python3 manage.py migrate
We will get output like this:
This means the SQL database server is now connected and Django automatically creates new tables.
Now let's create a superuser for the Django admin panel:
python3 manage.py createsuperuser
In this section, we have to give email and username, and password. Now we are set for the next part.
If you want to set up the Mysql database in your local server then go through this link.
In the 2nd part of this blog, we will create my portfolio app and create models and views for our main website.
This is the 2nd part of the project.
Reference:
- https://docs.djangoproject.com/en/3.2/ref/settings/
- https://www.digitalocean.com/community/tutorials/how-to-create-a-django-app-and-connect-it-to-a-database
- http://henriquefell.blogspot.com/2014/12/how-django-web-framework-works.html
- https://phoenixnap.com/kb/how-to-install-python-3-ubuntu
- https://medium.com/@jvdali966/understanding-djangos-work-flow-1ee521422092
- https://realpython.com/get-started-with-django-1/