Create a basic Ethereum Dapp using Rivet, Django, and Web3.py

Introduction

In our previous Python-oriented tutorial, you learned how to use Web3.py to query the Ethereum blockchain via your Rivet endpoint, but we didn’t get into how you can use that knowledge to build a Dapp. In this tutorial, that’s just what we’re going to do.

The Big Picture

  1. You’ll start by setting up a Rivet account if you haven’t already done so. If you have already done so and you are signed into Rivet, your key will appear here:

  2. Next, you’ll install all the necessary dependencies and configure them to use your Rivet endpoint.

  3. Finally, you’ll construct a basic Django application that displays information about the latest gas price and block number from the Ethereum blockchain.

1. Setting up Rivet

My Rivet endpoint: https://YourApiKey.eth.rpc.rivet.cloud/

If you see a URL above that does not contain “YourApiKey”, then you already have a Rivet account set up and you’re logged in. Nice work! Go ahead and skip to step 2.

If you don’t see a URL with your unique key in it, no worries! This will just take a moment:

First, navigate to the Rivet signup page.

When you get there, enter a nickname for your account and a valid email address to which you have access. Select submit.

Web3-savvy?

You can also create an account and top it up with credits via Web3 using the Login with Web3 button on the signup page.

Not sure what this is? Not to worry, just disregard this box for now. Pretty soon, this will make perfect sense.

On the next view, you’ll verify your email address using a code we sent to it when you selected submit. Check your email for a message from Rivet, copy the code, and paste it in.

Got it? Good.

Finally, acknowledge our terms of use—and if you have a promo code, don’t forget to add that too. (If you do forget, contact us at support@rivet.cloud and we’ll hook you up retroactively if you can tell us what the code was supposed to be.

Good to go? Great! Go ahead and hit submit, and voila! Your endpoint URL is:

https://YourApiKey.eth.rpc.rivet.cloud/

Still need help?

If you don’t see it, make sure you’re signed in. If you still don’t see it, please contact us at support@rivet.cloud and we’ll get to the bottom of it. If you recall it, please include the nickname you gave us when you signed up in your message.

Congratulations, you’re 1/3 of the way through this tutorial!

2. The Setup

If you did our Web3.py tutorial, you’ll be all set with pip3 and Web3.py. But if you haven’t, Web3.py can be installed using pip3 as follows:

$ pip3 install web3

Got it? Great. Now we need to install and configure Django.

To install Django, simply run:

$ python3 -m pip3 install Django

Once that process completes, you’re ready to create and configure a basic Django project. This can be done in a few simple steps. First, create a directory called hellorivet and cd into it. Then run:

$ django-admin startproject hellorivet

This creates all the directories and files you’ll need for a basic Django project. To see it working, you need to run a local server. Let’s try it! cd into hellorivet and run:

$ python3 manage.py runserver

This starts the project on a local server you can access in your browser at http://127.0.0.1:8000/. But it’s just running a placeholder page! Let’s fix that next by creating a view and mapping it to a url.

To create a view, cd to the hellorivet subdirectory and create a file called views.py. Open it, and add the following:

from django.http import HttpResponse

def index(request):
 return HttpResponse("Hello Rivet!")

We’ll be making some more edits here later, but for now save your changes. Next, we need to map the view to a URL—which is what urls.py is for! Open it, and add:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
 path('admin/', admin.site.urls),
 path('', views.index, name='index'),
 ]

Save your changes (and run the server again if you’re not still running it):

$ python manage.py runserver

Now, load the test page in your browser. See the text you entered in your view? Great! With that done, let’s integrate web3!

3. The Grand Finale

It’s time to integrate web3.py into your view and tie some blockchain queries into your dapp. To do so, update views.py as follows:

from web3 import Web3
from django.http import HttpResponse

rivet = 'https://YourApiKey.eth.rpc.rivet.cloud/'

web3 = Web3(Web3.HTTPProvider(rivet))


def index(request):
    gas_price = web3.eth.gasPrice
    block_number = web3.eth.blockNumber
    return HttpResponse(f"The curent estimated gas price in WEI is {gas_price} at block number {block_number}.")

Refresh your view, and you should see something like the following:

The curent estimated gas price in WEI is 21500001481 at block number 11368679.

Congratulations! You’ve done it!

Conclusion

As you’ve seen in this tutorial, with Django, you can use Web3.py to display the results of queries to the blockchain—but it can do so much more. With it, you can do everything from signing and sending transactions to deploying and interacting with contracts. Basically, anything you want to do with Ethereum, you can do with Django, Web3.py, and Rivet.

In future tutorials, we’ll expand from here to help you grow your creative repertoire. In the meantime, you should check out the resources available in the rest of this doc site, at the Django doc site, the Web3.py doc site, and at Ethereum.org and start dreaming up what you want to learn how to BUIDL.

Congratulations!! You’re now an Ethereum BUIDLer—welcome to the community!!

—❤️Rivet