|
| 1 | +# Django |
| 2 | + |
| 3 | +This example demonstrates how to configure and run a Django app using Devbox. It installs Python, PostgreSQL, and uses `pip` to install your Python dependencies in a virtual environment. |
| 4 | + |
| 5 | +[Example Repo](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/django) |
| 6 | + |
| 7 | +[](https://devbox.sh/github.com/jetpack-io/devbox-examples?folder=stacks/django) |
| 8 | + |
| 9 | +## How to Use |
| 10 | + |
| 11 | +1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/) |
| 12 | +1. Run `devbox shell` to install your packages and run the init_hook. This will activate your virtual environment and install Django. |
| 13 | +1. Initialize PostgreSQL with `devbox run initdb`. |
| 14 | +1. In the root directory, run `devbox run create_db` to create the database and run your Django migrations. |
| 15 | +1. In the root directory, run `devbox run server` to start the server. You can access the Django example at `localhost:8000`. |
| 16 | + |
| 17 | +## How to Create this Example from Scratch |
| 18 | + |
| 19 | +### Setting up the Project |
| 20 | + |
| 21 | +1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/). |
| 22 | +1. Run `devbox init` to create a new Devbox project in your directory. |
| 23 | +1. Install Python and PostgreSQL with `devbox install python python310Packages.pip openssl postgresql`. This will also install the Devbox plugins for pip (which sets up your .venv directory) and PostgreSQL. |
| 24 | +1. Copy the requirements.txt and `todo_project` directory into the root folder of your project |
| 25 | +1. Start a devbox shell with `devbox shell`, then activate your virtual environment and install your requirements using the commands below. |
| 26 | + |
| 27 | + ```bash |
| 28 | + source $VENV_DIR/bin/activate |
| 29 | + pip install -r requirements.txt |
| 30 | + ``` |
| 31 | + |
| 32 | + You can also add these lines to your `init_hook` to automatically activate your venv whenever you start your shell |
| 33 | + |
| 34 | + |
| 35 | +### Setting up the Database |
| 36 | + |
| 37 | +The Django example uses a Postgres 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. |
| 38 | + |
| 39 | +1. Initialize your Postgres database cluster with `devbox run initdb`. |
| 40 | + |
| 41 | +1. Start the Postgres service by running `devbox services start postgres` |
| 42 | + |
| 43 | +1. In your `devbox shell`, create the empty `todo_db` database and user with the following commands. |
| 44 | + |
| 45 | + ```bash |
| 46 | + createdb todo_db |
| 47 | + psql todo_db -c "CREATE USER todo_user WITH PASSWORD 'secretpassword';" |
| 48 | + ``` |
| 49 | + |
| 50 | + You can add this as a devbox script in your `devbox.json` file, so you can replicate the setup on other machines. |
| 51 | + |
| 52 | +1. Run the Django migrations to create the tables in your database. |
| 53 | + |
| 54 | + ```bash |
| 55 | + python todo_project/manage.py makemigrations |
| 56 | + python todo_project/manage.py migrate |
| 57 | + ``` |
| 58 | + |
| 59 | +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. |
| 60 | + |
| 61 | +### Running the Server |
| 62 | + |
| 63 | +You can now start your Django server by running the following command. |
| 64 | + |
| 65 | + ```bash |
| 66 | + python todo_project/manage.py runserver |
| 67 | + ``` |
| 68 | + |
| 69 | +This should start the development server. |
0 commit comments