Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Go into the project folder that was created when the repo was cloned.

  • Start the project. Be sure to enter the . at the end of the command line in order to create the project in the current folder. Otherwise, django will create a subfolder with the project name and place it one level down.

    Code Block
    django-admin startproject <project name> .
  • Set the ALLOWED_HOSTS setting in the settings.py file. For example:

    Code Block
    ALLOWED_HOSTS = ["ece-li-scheduling-2.eng.uwaterloo.ca", "localhost", "127.0.0.1"]
  • Optional - add a .gitignore file to the root project directory. Add the file db.sqlite3 to this .gitignore file so that the SQLite database does not get stored in the git repo.

Push first commit

  • Create new branch initialize. The project access token is not permitted to push to main branch.

    Code Block
    git branch initialize
    git checkout initialize
    git push --set-upstream origin initialize
  • Commit the initial files created by django for the project.

    Code Block
    git add .
    git commit -m "Initialize django project."
    git push
  • Merge the initialize branch into main branch.

...

At this stage, you should be able to use a browser to test that the Django server is up and running. Go to http://<server address>:8000.

Setup Docker Volume

A Docker volume is the preferred way of storing state information for the Docker container. For example, the db.sqlite3 file is best stored on a Docker volume, so that it can be referenced by any container. Therefore, as the Django project codebase develops, each version of the container can use the same data store.

  • To create the Docker volume, on the host:

    Code Block
    docker volume create <volume name>

When running the container, the volume is mounted using the -v switch. The syntax for this switch is:

Code Block
-v <volume name>:<path to mount>

Establishing database file

After the first time python3 manage.py migrate has been run,

  • Transfer the db.sqlite3 file to the mounted volume.

  • Delete the db.sqlite3 file from its default location at the project root folder.

  • Create a symlink at the project root folder called db.sqlite3 that points to the file on the mounted volume.

Database connection using entrypoint.sh

It is recommended that the entrypoint.sh script is used to establish the database connection. The general sequence in the entrypoint.sh script to connect the database on the volume is:

  • Run makemigrations/ migrate. This creates a db.sqlite3 database file in its default location in the project root folder.

  • Delete the db.sqlite3 file from the project root folder.

  • Create a symlink to the database folder.

Here is an example of an entrypoint.sh script:

Code Block
#!/bin/bash

# Ensure the log directory exists
mkdir -p /var/log/django

# Redirect stdout and stderr to the log file
exec >> /var/log/django/entrypoint.log 2>&1

python3 manage.py makemigrations
python3 manage.py migrate

rm /opt/ece_li_scheduling/db.sqlite3
ln -s /var/lib/ece_li_scheduling/db.sqlite3 /opt/ece_li_scheduling/db.sqlite3

python3 manage.py runserver 0.0.0.0:8000

Start First App

  • Navigate to the root directory of the Django project (where the manage.py file is located).

  • Use manage.py to initialize the app:

    Code Block
    python3 manage.py startapp <app name>

...

The app needs at least one view.

  • Add a function to the app views.py file with a name that matches the function name specified in the urls.py file. Here is an example,

    Code Block
    from django.http import HttpResponse
    
    def home(request):
        return HttpResponse("Hello, World!")