mkdir djangoku_dev
cd djangoku_dev
Starting the project
Now that all the prerequisites are fullfilled it is time to start the real work. We will create our project folder, a local Git repository and a virtual environment. Then we will add Django to the mix.
Beginning with the basics
Git will keep track of our file versioning and will be our gateway to distribute the files to GitLab and Heroku later. By using a virtual environment we have an isolated sandbox avoiding confusion with Python versions and modules that may be installed on the operating system already. Both Git and a virtual environment are mandatory for a streamlined Heroku workflow. But it is generally a good practice to start any Python projects that way.
In this book the project will be called "djangoku_dev" and you will see that the root path to the project is djangoku_dev .
Obviously you will have your custom path and maybe a more suitable project name – Keep in mind that you have to adjust some commands accordingly.
|
Create the project folder
Create a project folder and navigate with the terminal to it:
If you are on the Mac, there is an easy way to do this:
Create the project folder in the Finder, open a new Terminal window, write cd , drag the folder from the Finder into the Terminal window and hit Enter
|
Initiate a new Git repository
Initiate a new Git repository:
git init
It is a good idea to create a Readme file at the beginning of a project. We will create this file later when we push our repository to GitLab. Feel free to create it now and add information along the way. Do not add any sensitive data to it. |
Create the virtual environment
If you are confused about virtual environments (as I still am sometimes), check out this great article on Real Python to get an overview. |
In this book the virtual environment will be called virtualenv. You can name your virtual environment any way you want and adjust the commands accordingly. |
Create a new virtualenvironment in your project folder and activate it:
python3 -m venv virtualenv
source virtualenv/bin/activate
You can see that you have activated the virtual environment if the project foldername is between in parenthesis e.g. (virtualenv)
This virtual environment folder is needed only locally. GitLab and Heroku will create their own environments later. Therefore it is important not to track the virtual environment folder with Git.
Create a .gitignore file and add virtualenv to it:
echo virtualenv > .gitignore
Adding your first commit
Now that we have created the base layer for our project, we should do our first commit.
Stage all changed files – that’s just the .gitignore file as we ignored everything else – and commit them:
git add .
git commit -m "Start djangoku 🌱"
Django. Finally!
After all this preparation and groundwork it is finally time to add Django.
Install Django
Make sure the virtualenvironment is still activated by checking if the project folder is between in parenthesis.
Install the Django module:
pip install django
Start the Django project
Now that the Django module was installed, we can start the actual Django project.
Note that the command has a .
at the end – this will make sure we create the Django project in the current directory and not create a new one.
Start the Django project:
django-admin.py startproject djangoku .
If you look into your root project folder you should see that a file called manage.py and a djangoku folder were added.
Run Django:
python manage.py runserver
After you have run the command, you should see an address like this: http://127.0.0.1:8000/
.
Open this in your browser and see if Django is running successfully.
To stop the Django server press Ctrl+c while being in the Terminal.
Ignore the SQLite database
Django creates an SQLite database automatically. That’s the db.sqlite3 file in the root folder. We will not need this file and will delete it later, because we will work with a PostgreSQL database. For now it is enough to not track the database file and add it to .gitignore. When deleted without declaring an alternative database at the same time, Django just recreates db.sqlite3 everytime the server runs.
Note that the command has two >
, because we want to append a line to the .gitignore file.
There are other Django projects, where a SQLite database is good enough. Even then you should always ignore .sqlite3 files. |
echo *.sqlite3 >> .gitignore
Commit
Track the new files in Git:
git add .
git commit -m "Add Django project 🤠"
Freeze!
As long as we don’t push anything to the server we don’t have to bother that much about dependencies. Still this is a good moment to freeze the Python modules we are using at this stage into a requirements.txt file. With this file in our repository, GitLab and Heroku will automatically install the exact versions of Python modules we use in their environment.
Let’s create and fill the requirements.txt in one go:
pip freeze > requirements.txt
Stage and commit requirements.txt:
git add requirements.txt
git commit -m "Add current project requirements 📜"
Checklist
✔︎ Project is tracked via Git
git log
→ Outputs the last commit messages
✔︎ Virtual environment is activated
→ The root project folder is wrapped in parenthesis in the Terminal.
✔︎ Virtual environment uses the correct Python
which python
→ Outputs a path that leads into the virtual environment folder.
✔︎ Django is installed
python -m django --version
→ Outputs Django version.
✔︎ Django works
→ Visit the URL that Django showed on startup (usually http://127.0.0.1:8000
) and see if it shows the Django success message.