3.6. HELP! Django Shortcut#

The Django tutorial from this week is pretty complicated. Add in the other changes from my lab and there’s a lot that can go wrong. If you find yourself stuck without a way to move forward you can download and use a tested copy of the source.

These instructions take the place of:

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

Donwload My Django App (and Remove Yours)#

Here are steps to get you setup in your development environment:

  1. If you have a djangotutorial directory, remove it (or move it if you like):

    $ rm -rf djangotutorial
    
  2. Download and extract my reference copy:

    $ curl https://www.lifealgorithmic.com/_downloads/cda4d028b49e416ece3087ffee5344fd/djangotutorial.tar.gz | gunzip -c - | tar -xvf -  
    

    This will create a new djangotutorial directory with no database and no migrations.

  3. Make migrations and initialize the database:

    $ cd djangotutorial 
    $ python3 ./manage.py makemigrations
    $ python3 ./manage.py migrate
    
  4. Create and administrator account:

    $ python3 ./manage.py createsuperuser 
    
  5. Start the server and confirm you can see the front page and login:

    $ export DEBUG=1 
    $ python3 ./manage.py runserver