4. Develop

Before we deploy our project in a production-like fashion, let’s start the development mode first:

devspace dev

This command tells DevSpace to:

Build a Docker image based on the Dockerfile Tag and push this image to a Docker registry (e.g., Docker Hub) Deploy a Helm chart with a Kubernetes deployment for our Django app Start port-forwarding to access the application on localhost (plus open in browser) Stream the logs of your containers Watch for file changes and restart the app if any file changes

You can customize what happens within the DevSpace config file devspace.yaml , which devspace init created for your project.

After devspace dev has successfully deployed, you should see a log output of Django inside the command line, and the browser will open up with our Django site showing a beautiful welcome page:

Django Welcome Page — If you see this one, you’re doing everything right so far!

Now, it’s time to create the /pages folder. Run this command to create it:

devspace enter python manage.py startapp pages

After adding the pages folder, we need to add pages.apps.PagesConfig to the INSTALLED_APPS array within $PROJECT_NAME/settings.py :

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'pages.apps.PagesConfig',

]

Great! Time to add a Hello World page.

Add ‘Hello World’ view

Edit the file $PROJECT_NAME/pages/views.py , so that it looks like this:

from django.http import HttpResponse def homePageView(request):

return HttpResponse('Hello, World!')

Now, create the file $PROJECT_NAME/pages/urls.py with the following content:

from django.urls import path from .views import homePageView urlpatterns = [

path('', homePageView, name='home')

]

And the last step for adding our Hello World page is to change the code in $PROJECT_NAME/urls.py from:

from django.contrib import admin

from django.urls import path urlpatterns = [

path('admin/', admin.site.urls),

]

to:

from django.contrib import admin

from django.urls import path, include urlpatterns = [

path('admin/', admin.site.urls),

path('', include('pages.urls')),

]

That’s it! Now, you can reload the browser, and you will see that DevSpace updated your Django app in real-time — no need to deploy your project because DevSpace uses hot reloading to update your running containers.

Try it yourself:

Change your Hello World! statement inside the file: $PROJECT_NAME/pages/views.py Reload the browser and see how the change is taking effect immediately.

Bonus: add PostgreSQL database

Before you can add a Postgres database, make sure you terminate devspace dev , e.g., using CTRL+C .

First, we need to add the Postgres driver for Python to our requirements.txt using this command:

docker run --rm -v /$PROJECT_DIR:/app -w //app python bash -c "pip install psycopg2 && pip freeze | grep psycopg2 >> $PROJECT_NAME/requirements.txt"

Now we need to tell DevSpace that we also want to deploy a PostgreSQL database. To do this, we need to change the deployments section within our devspace.yaml from:

deployments:

- name: mysite # this line may be different for you

helm:

componentChart: true

values:

containers:

# this line may be different for you

- image: dscr.io/${DEVSPACE_USERNAME}/mysite

service:

ports:

- port: 8000

to:

deployments:

- name: database

helm:

chart:

name: stable/postgresql

values:

postgresqlDatabase: "db_mysite"

postgresqlUsername: "db_user"

postgresqlPassword: ${DB_PASSWORD}

- name: mysite # this line may be different for you

helm:

componentChart: true

values:

containers:

# this line may be different for you

- image: dscr.io/${DEVSPACE_USERNAME}/mysite

env:

- name: DB_HOST

value: "database-postgresql"

- name: DB_NAME

value: "db_mysite"

- name: DB_USER

value: "db_user"

- name: DB_PASSWORD

value: ${DB_PASSWORD}

service:

ports:

- port: 8000

If we deploy the project again, DevSpace will now start a PostgreSQL database container. However, before we do this, we need to configure our Django project to connect to the database.

So change the $PROJECT_NAME/settings.py from:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.sqlite3',

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

}

}

to:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.postgresql',

'HOST': os.environ.get('DB_HOST'),

'NAME': os.environ.get('DB_NAME'),

'USER': os.environ.get('DB_USER'),

'PASSWORD': os.environ.get('DB_PASSWORD'),

}

}

Now, let’s redeploy our Django project using this command:

devspace dev -b

The first time you run devspace dev after this change, DevSpace will ask you to define a database password that is used for the variable DB_PASSWORD .

Notice the -b flag, which is a shortcut for --force-rebuild . It tells DevSpace that we do not want to use hot reloading and instead to rebuild the Docker image and redeploy the project completely. This is often useful when important settings change that cannot be updated using hot reloading, e.g., settings.py .

When you see the logs again, you can either abort devspace dev or you can open an additional terminal and check the logs of the PostgreSQL database to make sure it is ready before you continue. You can check if it’s ready by taking a look at the logs via:

devspace logs -f

After adding the database connection to the Django app, we need to run a migration. We can do this using the following command:

devspace enter python manage.py migrate

DevSpace will ask you in which container the migration should be executed. Make sure to select the Django application.