3.3. Start a Django Application#
Django is a framework for writing web applications in Python. In this lab you’ll complete Parts 1 and 2 of the “Writing your first Django app” tutorial in your dev environment. The tutorial is fairly detailed because it’s oriented to new Django developers. There are some steps that aren’t absolutely necessary to get your application running. You should read the tutorial carefully so that you understand the structure of your starter application.
Important
The working directory should be the root of your project repository. If you used the default repo name this command takes you there:
$ cd ~/cis-92 # For Cloud Shell
$ cd /workspaces/cis-92 # For devcontainers
Setup Python#
Note
This lab assumes that you have the virtual environment you created in the 3.2. A Flask Application lab. Please complete that lab before you start this one
Confirm that you see (.venv) in your prompt. If not activate the virtual
environment like this:
$ source ./.venv/bin/activate
Notice that your prompt changes.
With your environment activated you use the pip package manager to install
Django:
$ pip install Django==6.0.1
Now Django is installed.
Git and Python Apps#
Python creates temporary files that aren’t supposed to be checked in. When you
have files that you never want to check in you specify their patterns in a file
called .gitignore in the root of your repository. Before you create or run a
Pyhton app you should have these ignore patterns:
venv*/
.venv/
*.pyc
*.sqlite3
Write Your First Django App#
Follow the instructions at this URL:
https://docs.djangoproject.com/en/6.0/intro/tutorial01/
When you’re done you should be able to log in to Django using the administrative
interface and see the polls. Later you’ll make it deployable by creating a
Dockerfile.
Testing Environments
Cloud Shell and Codespaces use a proxy to connect you with running apps. That
causes problems in Django. Before you can login to your new Django application
you must put this setting at the bottom of mysite/settings.py:
CSRF_TRUSTED_ORIGINS = ['https://*.cloudshell.dev',
'https://*.github.dev',
'http://localhost:8000']