visit
To start a new Django project, you need to use the django-admin
command. This command lets you create a project directory and some basic files for your Django application. For example, if you want to create a project called my_project
, you can run this command in your terminal:
django-admin startproject my_project
├──
The manage.py
file is a script that allows you to perform various tasks for your project, such as running the development server, creating database migrations, and testing your code. The project/
directory contains the settings and configuration files for your project.
The settings.py
file defines the main settings for your project, such as the database connection, the installed apps, and the middleware. The urls.py
file maps the URLs of your project to the views of your apps.
python manage.py runserver
manage.py
: A script that provides various commands for managing your project, such as creating apps, migrating the database, and testing your code.
project/
: A directory that contains the settings and configuration files for your project. The main files in this directory are:
`settings.py`: A file that defines the main settings for your project, such as the database connection, the installed apps, and the middleware. You can customize this file to suit your needs and preferences.
`urls.py`: A file that maps the URLs of your project to the views of your apps. You can define different URL patterns for different parts of your web application.
apps/
: A directory that contains all of the Django apps that make up your project. Each Django app is a separate Python package that provides a specific functionality or feature for your web application. You can create your own apps or use existing ones from third-party sources.
python manage.py runserver
python manage.py test
Ran 1 test in 0.001s
Ok
FROM python:3.9
#Install Django and other required packages
RUN pip install django
# Copy the Django project files into the image
COPY ./app
# Set the working directory
WORKDIR /app
# Start the Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
version: "3.9"
services:
web:
build:
ports:
-"8000:8000"
volumes
-./:/app
db:
image: postgres:14.0-alpine
volumes:
-./postgres:/var/lib/postgresql/data
This docker-compose.yml file defines two services: web
and db
. The web
service will build the Docker image from the Dockerfile that we created in the previous section. The web
service will also expose port 8000 on the host machine so that we can access the Django development server.
The db
service will use the official PostgreSQL Docker image. The db
service will also mount the postgres
directory on the host machine to the /var/lib/postgresql/data
directory inside the container. This will ensure that the database files are persisted between container restarts.
docker-compose up -d
You can also log in to the Django admin page at //localhost:8000/admin/
. The username and password are admin
and admin
by default.
- Go to your profile page, and click on the New button next to Repositories.
- Give your repository a name, such as django-docker-app
, and optionally add a description.
- Click on the Create repository button.
- Go to your Django project folder in the terminal, and type git status
to see the current state of your repository. You should see something like this:
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: Dockerfile
modified: docker-compose.yml
modified: requirements.txt
no changes added to commit (use "git add" and/or "git commit -a")
This means that you have some modified files that are not staged for commit. To stage them for commit, you need to use the git add
command.
- Type git add
. to stage all the modified files for commit. Alternatively, you can specify the file names that we want to stage, such as git add
Dockerfile docker-compose.yml
requirements.txt
.
- Type git status
again to see the updated state of our repository. You should see something like this:
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: Dockerfile
modified: docker-compose.yml
modified: requirements.txt
This means that you have some changes that are staged for commit. To commit them, you need to use the git commit
command.
- Type git commit -m "Add Docker configuration files"
to commit the staged changes with a message describing what we did. Alternatively, you can omit the -m
flag and enter a longer message in an editor that will open after typing git commit
.
- Type git status
again to see the final state of our repository. You should see something like this:
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
This means that you have one commit that is not pushed to the remote repository yet. To push it, you need to use the git push
command.
- Type git push origin main
to push your local commit to the remote repository on GitHub.
Alternatively, you can specify the remote name and branch name that you want to push, such as git push origin main
.
- Go to and click on the Signup button.
- Fill in the required information, such as name, email, password, etc. - Choose a primary development language, such as Python.- Click on the Create Free Account button.
- Check our email and click on the link to verify our account.
- Open a terminal, and type heroku --version
to verify that the installation was successful. You should see something like this:
heroku/7.59.0 win32-x64 node-v12.21.0
- Go to your Django project folder in the terminal and type heroku login
to log in to your Heroku account using the Heroku CLI tool. You may be asked to enter your email and password or use a web browser to authenticate yourself.
- Type heroku create django-docker-app
to create a new Heroku app with the name django-docker-app
. Alternatively, you can omit the name and let Heroku generate a random name for you. You should see something like this:
Creating ⬢ django-docker-app... done
//django-docker-app.herokuapp.com/ |
//git.heroku.com/django-docker-app.git
- Type heroku config:set SECRET_KEY=<your_secret_key>
to set an environment variable for the SECRET_KEY setting of our Django project. You need to replace <your_secret_key>
with a random string that you can generate using a tool like .
Alternatively, you can use the existing secret key that you have in your settings.py file, but this is not recommended for security reasons.
- Type heroku addons:create heroku-postgresql:hobby-dev
to add a free PostgreSQL database add-on to your Heroku app. This will create a new database for your Django project and set an environment variable for the DATABASE_URL setting of your Django project. You should see something like this:
Creating heroku-postgresql:hobby-dev on ⬢ django-docker-app... free
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-curved-12345 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation
This means that you have added a PostgreSQL database add-on with the name PostgreSQL-curved-12345 and the URL DATABASE_URL.
- Type heroku config
to see the list of environment variables that you have set for your Heroku app. You should see something like this:
=== django-docker-app Config Vars
DATABASE_URL: postgres://<username>:
<password>@<host>:<port>/<database>
SECRET_KEY: <your_secret_key>
This means that you have two environment variables, DATABASE_URL and SECRET_KEY, that you can use in your Django project settings.
How to deploy your Django project to Heroku using the `heroku
` command?
To deploy our Django project to Heroku using the heroku
command, you need to follow these steps:
- Go to your Django project folder in the terminal, and type heroku container:login
to log in to the Heroku Container Registry using the Heroku CLI tool. This will allow you to push our Docker image to Heroku.
- Type heroku container:push web -a django-docker-app
to build and push your Docker image to Heroku. You need to specify the name of your Heroku app, which is django-docker-app in this case. You should see something like this:
=== Building web (Dockerfile)
Sending build context to Docker daemon 1.024kB
Step 1/9 : FROM python:3.9-slim
---> 7f5b6ccd03e9
Step 2/9 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> 64b5d0e40a22
Step 3/9 : RUN mkdir /code
---> Using cache
---> 4d8c638f2b6c
Step 4/9 : WORKDIR /code
---> Using cache
---> e69c02a028cd
Step 5/9 : COPY requirements.txt /code/
---> Using cache
---> 8f0f3e0f2d8c
Step 6/9 : RUN pip install -r requirements.txt
---> Using cache
---> 0f7b497d81ed
Step 7/9 : COPY . /code/
---> Using cache
---> c0a8e9a32b16
Step 8/9 : EXPOSE 8000
---> Using cache
---> a1d36a4a2da4
Step 9/9 : CMD ["gunicorn", "django_docker.wsgi", "--bind", "0.0.0.0:8000"]
---> Using cache
---> f7f3c0418a1d
Successfully built f7f3c0418a1d
Successfully tagged registry.heroku.com/django-docker-app/web:latest
=== Pushing web (Dockerfile)
The push refers to repository
[registry.heroku.com/django-docker-app/web]
f7f3c0418a1d: Pushed
latest: digest:
sha256:6cbbf22cf6aa60e0343e6d8e7c4c2eeb2e
cb8fd5e82a42dfe5f4aeeb15af89ec size: 528
Your image has been successfully pushed. You can now release it with the 'container:release' command.
- Type heroku container:release web -a django-docker-app
to release our Docker image to our Heroku app. You should see something like this:
Releasing images web to django-docker-app... done
- Type heroku run python manage.py migrate -a django-docker-app
to run the database migrations on your Heroku app. This will create the necessary tables and indexes for your Django project on the PostgreSQL database. You should see something like this:
Running python manage.py migrate on ⬢ django-docker-app... up, run.1234 (Free)
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, polls
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying polls.0001_initial... OK
Applying sessions.0001_initial... OK
- Type heroku open -a django-docker-app
to open your deployed web application in a web browser. You should see your Django project running on Heroku.
You have successfully deployed your Django project to Heroku using the heroku
command. You can now enjoy your web application and share it with others.
Read Next: CI/CD is essential for fast and reliable software delivery. To optimize your CI/CD pipeline for maximum efficiency, choose the right tools, streamline your workflow, use automated testing and QA, parallelize builds, use monitoring and feedback loops, perform security checks, and continuously improve your pipeline. Read More
Read Also: Everything We Learned at DockerCon 2023 Read More