How to set up Django website with jQuery

In the end, it turned out to be very easy. This is all one has to do:

First, make a folder named static inside your folder app:

mySite
---mytemplates
---mySite
---myApp
------static

Then download jQuery from their site here. You click ‘Download’ and it will take you to a different page with all the code. Left click on the code and select ‘save as…’. Save it in the static folder.

Inside your settings.py file make sure that django.contrib.staticfiles is under INSTALLED_APPS (it is there by default).

Lastly, inside your base page have

<script type="text/javascript" src="https://stackoverflow.com/questions/12031825/{{ STATIC_URL }} /static/jquery-1.8.0.js">
</script> 
//Make sure that the jQuery name is correct. With updates and different versions, 
//the number after 'jquery' will change 

And now you can use jQuery throughout your site! (as long as the pages extend your basepage. If they don’t, they will need the above piece of code in their html.)

This works for me while working on my local machine. I haven’t tried actually deploying my site yet, so I hope this will still work.

Leave a Comment