Django

Framework
Django
Category
Full Stack
Publisher
Jetify
{
  "packages": {
    "python": "latest",
    "openssl": {
      "version": "latest",
      "outputs": ["out", "dev"]
    },
    "postgresql": "latest",
    "zlib":       "latest"
  },
  "env": {
    "PGPORT": "5434"
  },
  "shell": {
    "init_hook": [
      ". $VENV_DIR/bin/activate",
      "pip install -r requirements.txt --use-pep517"
    ],
    "scripts": {
      "create_db": [
        "echo \"Creating DB\"",
        "dropdb --if-exists todo_db",
        "createdb todo_db",
        "psql todo_db -c \"CREATE USER todo_user WITH PASSWORD 'secretpassword';\"",
        "psql todo_db -c \"ALTER DATABASE todo_db OWNER TO todo_user;\"",
        "psql todo_db -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO todo_user;\"",
        "python todo_project/manage.py makemigrations",
        "python todo_project/manage.py migrate"
      ],
      "initdb": [
        "initdb"
      ],
      "server": [
        "python todo_project/manage.py runserver"
      ],
      "test": [
        "initdb",
        "devbox services start",
        "devbox run create_db",
        "python todo_project/manage.py test",
        "devbox services stop"
      ]
    }
  }
}

Django Example

Built with Devbox

Open In Devbox.sh

How to Use

  1. Install Devbox
  2. Run devbox shell to install your packages and run the init_hook. This will activate your virtual environment and install Django.
  3. Initialize PostgreSQL with devbox run initdb.
  4. Start the Postgres service by running devbox services up postgresql. You can start it in the background using devbox services up -b postgresql.
  5. In the root directory, run devbox run create_db to create the database and run your Django migrations
  6. In the root directory, run devbox run server to start the server. You can access the Django example at localhost:8000

How to Create this Example from Scratch

Setting up the Project

  1. Install Devbox.

  2. Run devbox create --template django to create a new Devbox project in your directory.

  3. Install Python and PostgreSQL with devbox install. This will also install the Devbox plugins for pip (which sets up your .venv directory) and PostgreSQL.

  4. Copy the requirements.txt and todo_project directory into the root folder of your project

  5. Start a devbox shell with devbox shell. This will activate your virtual environment and install your requirements using the commands below.

    . $VENV_DIR/bin/activate
    pip install -r requirements.txt

    These lines are already added to your init_hook to automatically activate your venv.

Setting up the Database

The Django example uses a database. To set up the database, we will first create a new PostgreSQL database cluster, create the todo_db and user, and run the Django migrations.

  1. Initialize your Postgres database cluster with devbox run initdb.

  2. Start the Postgres service by running devbox services start postgres

  3. In your devbox shell, create the empty todo_db database and user with the following commands.

    createdb todo_db
    psql todo_db -c "CREATE USER todo_user WITH PASSWORD 'secretpassword';"

    You can add this as a devbox script in your devbox.json file, so you can replicate the setup on other machines.

  4. Run the Django migrations to create the tables in your database.

    python todo_project/manage.py makemigrations
    python todo_project/manage.py migrate

Your database is now ready to use. You can add these commands as a script in your devbox.json if you want to automate them for future use. See create_db in the projects devbox.json for an example.

Running the Server

You can now start your Django server by running the following command.

python todo_project/manage.py runserver

This should start the development server.

Related Docs

README.md

Level up your dev environment

No need to switch your style. Devbox works with any coding language, so you can get results as you write.
PREPARE FOR LAUNCH

Plans for teams of any size

Jetify Cloud offers pricing plans for Solo Developers to Large Enterprises, and everything in between. All plans include free credits, and you are only billed for what you use. Need something custom? Contact our team and we can help.
YOUR INPUT COUNTS

Join the community

Discuss ideas, uses and builds with the Jetify team and others.