[Ethereum] Integrating web3 and Django

djangojavascriptpythonweb3js

I'm trying to integrate web3 into a website which uses Django.

This snippet of code is not working

  <script type="text/javascript">
      document.getElementById("Mining").innerText = "Test";
      var Web3 = require('web3');
      var web3 = new Web3();
      web3.setProvider(new web3.providers.HttpProvider("http://146.169.45.149:9002"));
      var mining = web3.eth.mining;
  </script>
</body>

When I place the line document.getElementById("Mining").innerText = "Test"; under the var web3 = require('web3'); it stops working, so I'm guessing the problem should be there.

I've included both scripts for web3

<!-- web3 scripts -->
<script type = "text/javascript" src="/static/poll/web3-light.js"></script>
<script type = "text/javascript" src="/static/poll/bignumber.js"></script>

Thanks.

Best Answer

This is hard to debug without seeing your actual code. If it's falling over on the require line it's most likely failing to load the web3 javascript file, or loading it after you've placed your script. Look in the developer console of your browser and see if it's telling you anything, and also whether it's making the network requests for the JavaScript files and whether they're being found. Django could be serving them from there, but only if you've set the static/ path correctly and actually put the files there.

Related Topic