Skip to content

Commit f51c5fa

Browse files
authored
Add Django Example Docs (#676)
## Summary Add a Django Example to the Docs ## How was it tested? Localhost
1 parent d5cfebb commit f51c5fa

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

docs/app/docs/devbox_examples/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ You can view the full list of examples in our [Example Repo](https://github.com/
3333
## Full Stack Examples
3434
These examples combine configuration from multiple examples to create a full stack for development and deployment.
3535

36-
* [Drupal](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/drupal)
37-
* [Jekyll](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/jekyll)
38-
* [LAPP (Linux, Apache, PostgreSQL, PHP)](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/lamp-stack)
39-
* [LEPP (Linux, NGINX, PostgreSQL, PHP)](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/lemp-stack)
40-
* [Ruby on Rails](https://github.com/jetpack-io/devbox-examples/tree/main/stacks/rails)
36+
* [Django](stacks/django.md)
37+
* [Drupal](stacks/drupal.md)
38+
* [Jekyll](stacks/jekyll.md)
39+
* [LAPP (Linux, Apache, PostgreSQL, PHP)](stacks/lapp.md)
40+
* [LEPP (Linux, NGINX, PostgreSQL, PHP)](stacks/lepp.md)
41+
* [Ruby on Rails](stacks/rails.md)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
[![Open In Devbox.sh](https://jetpack.io/img/devbox/open-in-devbox.svg)](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.

docs/app/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ const sidebars = {
270270
label: 'Stacks',
271271
collapsed: true,
272272
items: [
273+
{ type: 'doc', id: 'devbox_examples/stacks/django' },
273274
{ type: 'doc', id: 'devbox_examples/stacks/drupal' },
274275
{ type: 'doc', id: 'devbox_examples/stacks/jekyll' },
275276
{ type: 'doc', id: 'devbox_examples/stacks/lapp' },

0 commit comments

Comments
 (0)