thirdweb

Mint an NFT in your Django app

Mint an NFT in your Django app

Published on:

December 12, 2021

Integrate thirdweb's SDK in a Django app

Raza Zaidi

Intro

In this guide we'll integrate third web's sdk into a Django app by adding a minting functionality. The beauty of this is that we'll only need Python! OK...and a bit of html. Does that count? Side note, we won't create a django project from scratch, but show how the thirdweb sdk works inside Django. If you get stuck, make sure to drop by our Discord!

Check out the GitHub repo here.

Dashboard setup

The first thing we need to do is head over to the dashboard and create a project and an NFT Collection module. if you don't know how to do this, check out this guide.

Once your module has been created, it's time to mint our first NFT, but we don't need the dashboard for that! I mean we could, but why use GUI if we can use Python!?

Finished setting up the module

Finished setting up the module

The code

Virtual-env

Make sure you have third web's sdk installed in your virtual environment.

#create a virtual environment python3 -m venv ./myenv #activate virtual environment source ./myenv/bin/activate #install the thirdweb sdk pip install thirdweb-sdk

Conda

#create a virtual environment conda create -n "thirdweb" python=3.9 #activate virtual environment conda activate thirdweb #install the thirdweb sdk pip install thirdweb-sdk

Integrating the SDK in Django

1. Our Django project

The Django structure looks like this. All I did was create the basic Django project with a Django app inside. This project is called nft_creator and the app is called minter. Navigate to your Django App and create a python file for the thirdweb SDK. I called mine nft_network.py.

Django folder structure

Django folder structure

2. Import the SDK

In order to make use of our SDK, you need to establish a connection to a blockchain. Click here to learn how to connect to the blockchain and instantiate our SDK.

For this guide, you will need to instantiate the SDK following these steps, using your own signer with thirdweb.

3. Define which module to use

Now we need to define which module we want to use. This is the module we just created inside our project PythonNFT. We called the module Python. This module has an address. We need to pass the address here. You can find the address here, under Python:

In our nft_network.py we include the following code. First we define a variable nft_smart_contract_address and store our module address in here.

Then we create an object that uses the Nft Collection module inside the sdk with our address.

The NFT collection module

The NFT collection module

nft_network.py
#pick your module and enter the smart contract address nft_smart_contract_address = "0xdd25FAEE772FbB1bcB7ba0b2cEE6387A8F82f032" nft_module = sdk.get_nft_module(nft_smart_contract_address)

4. Time to mint the NFT!

The arguments passed over here are the same as minting an nft inside the dashboard. If you want to familiarize yourself with the process, check out the dashboard!

Now open your views.py file and include the following code. We first handle the request from our HTML form (taking the image) and then pass the image on to the mint function. When the mint is successful you're redirected to another page.

views.py
from django.shortcuts import redirect, render, HttpResponse from . import nft_network from thirdweb import ThirdwebSdk, SdkOptions, MintArg def home(request): file_url = nft_network.nft_module.balance() if request.method == 'POST' and request.FILES['home']: upload = request.FILES['home'] name_nft = "Wing_02" description_nft = "My-Wing" image_nft = upload.read() prop = {} nft_network.nft_module.mint(MintArg(name=name_nft,description=description_nft,image_uri=image_nft, properties=prop)) return redirect("success") return render(request, "index.html", {'file_url': file_url}) def success(): return HttpResponse("successfully uploaded")

5. Using a HTML form

If you go to the GitHub repo, you might have noticed that I use a html form instead of a Django form. I did this, because some projects might use Django solely for the backend and html forms at the front. However feel free to use Django forms, the process would be essentially the same!

6. That's it!

Easy right? We have a ton of other functions and modules you can use. Check out our portal or our dashboard! Drop by our discord if you have any questions or need help.


Ready to build your first web3 app? Get early access & add web3 features to your project today.

Raza Zaidi

Contents

Intro

Dashboard setup

The code

Virtual-env

Conda

Integrating the SDK in Django

1. Our Django project

2. Import the SDK

3. Define which module to use

4. Time to mint the NFT!

5. Using a HTML form

6. That's it!