<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adam Bureš</title>
    <description>The latest articles on DEV Community by Adam Bureš (@adamburesxd).</description>
    <link>https://dev.to/adamburesxd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1103856%2F677fceb5-887c-4fb3-a490-4372632b90e6.jpg</url>
      <title>DEV Community: Adam Bureš</title>
      <link>https://dev.to/adamburesxd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adamburesxd"/>
    <language>en</language>
    <item>
      <title>Change the Web3 world with Django and Spheron</title>
      <dc:creator>Adam Bureš</dc:creator>
      <pubDate>Sun, 18 Jun 2023 19:20:11 +0000</pubDate>
      <link>https://dev.to/adamburesxd/change-the-web3-world-with-django-and-spheron-44ff</link>
      <guid>https://dev.to/adamburesxd/change-the-web3-world-with-django-and-spheron-44ff</guid>
      <description>&lt;p&gt;In the fast-changing world of &lt;strong&gt;Web3&lt;/strong&gt; 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 &lt;strong&gt;Django&lt;/strong&gt; for our purposes today, a simple app will be just enough. Second, you will need &lt;strong&gt;DockerHub&lt;/strong&gt; account. We will need to dockerize our app to put it on the Spheron Compute network. And the last thing is a &lt;strong&gt;Spheron&lt;/strong&gt; account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Django app
&lt;/h2&gt;

&lt;p&gt;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. &lt;br&gt;
We will install it using PIP.&lt;br&gt;
&lt;code&gt;pip install Django&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that we have Django, we should create a Django project. We can create a Django project. We are going to use this command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;django-admin startproject django_server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create apps for our project we will go to our project directory and then in the command line start a new app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python manage.py startapp myapp&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;Every great website has some content and our will not be different. Open the &lt;strong&gt;django_server/urls.py&lt;/strong&gt; and replace the code with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.urls import path
from myapp.views import hello_world

urlpatterns = [
 path('hello/', hello_world),
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will head into &lt;strong&gt;myapp/views.py&lt;/strong&gt; and add our view function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.http import HttpResponse

def hello_world(request):
   return HttpResponse("Hello, World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To try if our app is running without errors we can run our development server with this command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;python manage.py runserver&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open your web browser and visit it here:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://127.0.0.1:8000/hello/"&gt;http://127.0.0.1:8000/hello/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;strong&gt;requirements.txt&lt;/strong&gt;. 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.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Django==3.2.4&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Dockerfile
&lt;/h2&gt;

&lt;p&gt;To place our app on the DockerHub we will need to create a DockerHub account on &lt;a href="https://hub.docker.com/"&gt;https://hub.docker.com/&lt;/a&gt; and then to create a dockerfile.&lt;/p&gt;

&lt;p&gt;Our dockerfile will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can build the Docker image using this command:&lt;br&gt;
&lt;code&gt;docker build -t django_server .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will wait a bit until the build is done. When the build is done you can run the container using this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -p 8000:8000 django_server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When we have a working docker image of our Django app we will now push the app to DockerHub.&lt;br&gt;
To create a repository on Docker Hub:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sign up(opens in a new tab) or Sign in to Docker Hub(opens in a new tab).&lt;/li&gt;
&lt;li&gt;Select the Create Repository button.&lt;/li&gt;
&lt;li&gt;For the repo name, use django_server. Make sure the Visibility is Public.&lt;/li&gt;
&lt;li&gt;Select the Create button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To push the image we will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Login to the Docker Hub using the command docker login -u YOUR-USER-NAME.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;br&gt;
&lt;code&gt;docker tag django_server YOUR-USER-NAME/django_server&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;docker push YOUR-USER-NAME/django_server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is how it can look on the DockerHub:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yZ0yKi2---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1on5zfksue0bbs6776e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yZ0yKi2---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1on5zfksue0bbs6776e.png" alt="Docker Image on DockerHub" width="800" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Run on Spheron Compute
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click New Cluster on the top right corner.&lt;/li&gt;
&lt;li&gt;Select Import from Docker Hub.&lt;/li&gt;
&lt;li&gt;Enter the names for your cluster and docker image.&lt;/li&gt;
&lt;li&gt;Then, Add the tag and Click Next.&lt;/li&gt;
&lt;li&gt;Select the instance plan that suits your needs and Click Select Plan.&lt;/li&gt;
&lt;li&gt;Create new Port Mapping. Add the container port, and Select the exposed port you want to map it to. Click here to know more.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;You can add advanced configuration if required. Click here to know more.&lt;/li&gt;
&lt;li&gt;Click Deploy to initiate deployment.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://www.djangoproject.com/"&gt;here&lt;/a&gt; and if you are interested in the Spheron you can learn more &lt;a href="https://docs.spheron.network/"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>web3</category>
      <category>spheron</category>
      <category>soss</category>
    </item>
  </channel>
</rss>
