4.1. 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.
Before You Begin#
You should install Django in your dev environment. Django is a Python package so it can be installed with the pip
package manager:
$ pip install Django==4.2.9
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*/
*.pyc
*.sqlite3
Write Your First Django App#
Follow the instructions at this URL:
https://docs.djangoproject.com/en/4.2/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
.
Django’s Database
In part 2 of the tutorial you have to select a database engine for Django to use. You should keep ENGINE
to the default, django.db.backends.sqlite3
, for now. Later we will migrate to another engine.
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',
'https://localhost:8000']