I have run into a bit of a hiccup here.
I have a script which gets the user's Metamask wallet address which works fine. The problem arises when I need to save that wallet address to the user model in this field:
ethereum_address = models.CharField(max_length=42, blank=True, null=True)
I have a Javascript that fetches the wallet when the Connect button is pressed:
function connect() {
ethereum
.request({ method: 'eth_requestAccounts' })
.then((account)=> saveAccount(account))
.catch((error) => {
if (error.code === 4001) {
// EIP-1193 userRejectedRequest error
console.log('Please connect to MetaMask.');
} else {
console.error(error);
}
});
}
function saveAccount(account) {
console.log(account);
$.ajax({
url: '/connect-metamask/',
type: 'POST',
data: {
'account': account,
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
});
}
In the views.py I have this when I have a POST request:
def connect_metamask(request):
user = request.user
if request.method == "POST":
user.ethereum_address = request.POST.get("account")
user.save()
.........
But when I look at the database the ethereum_address is Null .
How do I make this work?