Using Python's Django framework in the cloud
Tags: webdev, python, learning
Published: 2019-05-21 17:21:57

---
title: Using Python's Django framework in the cloud
published: true
description: How to instantly start a coding sandbox in your browser to explore Python's Django web framework.
tags: webdev,python,learning
cover_image: https://thepracticaldev.s3.amazonaws.com/i/pbm1bosqot3g4vvc2xvk.png
canonical_url: https://next.tech/sandbox/uses/web-development-use-django-in-the-cloud
series: Next Sandbox
---

[Django](https://www.djangoproject.com/) is a Python framework for building web apps. Their website describes it like so:

> *Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It's free and open source.*

In this post, we'll cover how to use the Next Sandbox to get started with Django without downloading anything! We'll walk through how you can launch a Python sandbox and then install Django in it.

First, click [here](https://nt.dev/python) to launch a Python sandbox (you'll need to create a free Next Tech account if you haven't already).

Once your sandbox has loaded, run the following commands in your sandbox's terminal to install Django:

```bash
sudo apt update
sudo apt install -y python3-django
```

Then, create a new Django project:

```bash
django-admin startproject project .
```

Now you can run the initial Django migrations:

```bash
python3 manage.py migrate
```

Migrations in Django are explained in detail [here](https://docs.djangoproject.com/en/2.2/topics/migrations/).

To start the Django web server, run the following in your terminal:

```bash
python3 manage.py runserver 0.0.0.0:8000
```

Note the use of the `0.0.0.0:8000`. This is required to allow remote connections, which you will technically be making to your sandbox.

You'll also need to make some changes to the `project/settings.py` file. First, replace the contents of the `MIDDLEWARE` list with this:

```python
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware'
]
```

This removes an option added by default that prevents your webpage from being embedded, which it needs to be so the in-sandbox web browser can load it.

Then, you need add this line to the file:

```python
ALLOWED_HOSTS = ['*']
```

This will allow any host to connect so the domain your sandbox is running on will be able to.

This part of your file should now look something like this:

![](https://thepracticaldev.s3.amazonaws.com/i/tdij5pnt2nzo04qnesg3.png)

Okay, all set! Now, click the `+` and select a browser, then enter `localhost:8000` in the URL bar. You'll be shown this:


![](https://thepracticaldev.s3.amazonaws.com/i/i37dy11pcwc46rq4chul.png)

Great work! You can now continue to explore Django. I recommend reading through the [Django Quick Start guide](https://docs.djangoproject.com/en/2.2/intro/tutorial01/), which covers some of what we have in this post, and much more!

Have a question or comment? Drop us a note below!