Change the Web3 world with Django and Spheron
Tags: webdev, web3, spheron, soss
Published: 2023-06-18 19:20:11

In the fast-changing world of **Web3** you should know how to turn your ideas into reality. That is why I am going to tell you today how you can put your ideas up on the Web3. You will need three things. First, you will need an app in **Django** for our purposes today, a simple app will be just enough. Second, you will need **DockerHub** account. We will need to dockerize our app to put it on the Spheron Compute network. And the last thing is a **Spheron** account.

## Create Django app

First, we will start with the Django app. Then, you will have to install Django on your computer so we can actually make some projects. 
We will install it using PIP.
`pip install Django`

Now that we have Django, we should create a Django project. We can create a Django project. We are going to use this command: 

`django-admin startproject django_server`

To create apps for our project we will go to our project directory and then in the command line start a new app.

`python manage.py startapp myapp`

To connect the app with our project we will need to head into the **django_server/settings.py** and add **myapp** to the **INSTALLED_APPS** and add * to the **ALLOWED_HOSTS** list.

Every great website has some content and our will not be different. Open the **django_server/urls.py** and replace the code with the following: 

```
from django.urls import path
from myapp.views import hello_world
 
urlpatterns = [
 path('hello/', hello_world),
]
```
Then we will head into **myapp/views.py** and add our view function:

```
from django.http import HttpResponse
 
def hello_world(request):
   return HttpResponse("Hello, World!")
```

To try if our app is running without errors we can run our development server with this command: 

`python manage.py runserver`

Open your web browser and visit it here:

[http://127.0.0.1:8000/hello/](http://127.0.0.1:8000/hello/)

Perfect! Now that we have our running Django app we can start thinking how to put it on the Spheron Compute? Well it is easy. First we will need to create **requirements.txt**. We will need this file to pass the modules we need to have our application running smoothly. For now it is just the Django module.

`Django==3.2.4`

## Create a Dockerfile

To place our app on the DockerHub we will need to create a DockerHub account on [https://hub.docker.com/](https://hub.docker.com/) and then to create a dockerfile.

Our dockerfile will look something like this: 

```
# Use an official Python runtime as the base image
FROM python:3.9
 
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
 
# Set the working directory in the container
WORKDIR /code
 
# Install dependencies
COPY requirements.txt /code/
RUN pip install --no-cache-dir -r requirements.txt
 
# Copy the Django project code to the container
COPY . /code/
 
# Expose the port that Django runs on
EXPOSE 8000
 
# Run the Django development server
CMD python manage.py runserver 0.0.0.0:8000
```
Now we can build the Docker image using this command:
`docker build -t django_server .`

We will wait a bit until the build is done. When the build is done you can run the container using this command:

`docker run -p 8000:8000 django_server`

When we have a working docker image of our Django app we will now push the app to DockerHub.
To create a repository on Docker Hub:

1. Sign up(opens in a new tab) or Sign in to Docker Hub(opens in a new tab).
2. Select the Create Repository button.
3. For the repo name, use django_server. Make sure the Visibility is Public.
4. Select the Create button.

To push the image we will:
1. Login to the Docker Hub using the command docker login -u YOUR-USER-NAME.
2. Use the docker tag command to give the django_server image a new name. Be sure to swap out YOUR-USER-NAME with your Docker ID.
`docker tag django_server YOUR-USER-NAME/django_server`

3. Now try your push command again. If you’re copying the value from Docker Hub, you can drop the tagname portion, as you didn’t add a tag to the image name. If you don’t specify a tag, Docker will use a tag called latest.

`docker push YOUR-USER-NAME/django_server`

Here is how it can look on the DockerHub:

![Docker Image on DockerHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1on5zfksue0bbs6776e.png)

## Run on Spheron Compute
Now the fun part begins. Remember to set your docker image to public as Spheron can not access your private docker images. To run your app on Spheron:
1. Click New Cluster on the top right corner.
2. Select Import from Docker Hub.
3. Enter the names for your cluster and docker image.
4. Then, Add the tag and Click Next.
5. Select the instance plan that suits your needs and Click Select Plan.
6. Create new Port Mapping. Add the container port, and Select the exposed port you want to map it to. Click here to know more.
7. Add Environment Variables if any. Use the Secret Key toggle if the value is a secret key. When you enable the secret key toggle, it will not be saved in the database. Click here to know more.
8. Select your preferred Region if any. If you do not add a region, the container will be deployed in any region. Click here to know more.
9. You can add advanced configuration if required. Click here to know more.
10. Click Deploy to initiate deployment.

## Conclusion
YOU DID IT! You can now start celebrating as you are officially a Web3 developer! Congrats! There is still so much you have to learn but with this knowledge you will become a profesional in no time. If you are more interested in the Django framework you can learn more [here](https://www.djangoproject.com/) and if you are interested in the Spheron you can learn more [here](https://docs.spheron.network/).