The app is based on the official Writing your first Django app tutorial. You can see it deployed on Toolforge.
For simplicity, this app uses a sqlite database. In production, you would want to configure and use ToolsDB. Rememeber to change the settings in mysite/settings.py
-
Fork and clone this repository, then cd into the project folder
-
Create and activate a virtual environment
-
Install the dependencies:
pip install -r requirements.txt
-
Make sure these environment variables are available to the application via your preferred method:
- DJANGO_SECRET_KEY
- PROD: 0 for development, 1 for production
If you save these variables to a file, make sure to add it to your
.gitignore
. -
Run the migrations:
python manage.py migrate
This will create adb.sqlite3
file in the project root
This assumes you have previously created a tool in Toolforge with the name django-test
. Change this name to something else if you actually want to deploy your own copy, as a tool with this name already exists.
- log in to Toolforge
become django-test
toolforge build start https://github.com/blancadesal/sample-django
- Wait for the build to complete. You can check the status with
toolforge build show
. When finished, the status should show 'ok (Succeeded)' - If needed, check the build logs with
toolforge build logs
When the build has finished successfully, it's time to deploy. We will first set up the environment variables, then start the webservice.
- For each environment variable needed in production, run the following command:
toolforge envvars create <NAME> <VALUE>
- Check that the environment variables exist:
toolforge envvars list
You can also change the variables once the webservice is running, but you will have to restart it manually for the changes to take effect.
The sqlite database will be created in the tool's home directory (/data/project/<tool-name>
).
The application has access to this dir at runtime.
To apply the migrations, run:
toolforge jobs run --image tool-<tool-name>/tool-<tool-name>:latest --command "migrate" --no-filelog --wait migrate
This uses the Toolforge jobs framework.
The 'migrate' after --command
refers to the migrate
process in the Procfile
and runs python manage.py migrate
.
toolforge webservice buildservice start
- Wait for the webservice to spin up, then check the logs to see if everything went ok:
toolforge webservice logs -f
- Navigate to https://app-name.toolforge.org/polls/ to verify the application is working as expected. You will see a page with the text "No polls are available". This is expected, as the db starts out empty.