Reputation: 2738
I am trying to implement the code in django. My code works fine normally. But, when I try to use in django. It seems javascript not working. Why it is not working in django ? How can I run a html page that uses the javascript ?
*The html page lives in my template folder and in this folder, I have also a folder like scripts/
.
Upvotes: 0
Views: 4041
Reputation: 10685
*The html page lives in my template folder and in this folder, I have also folder like scripts/spryMap-2.js
You should not have your static files (eg spryMap-2.js) in your templates folder. It should go in your static files folder. Then you load your script with something like this
<script type="text/javascript" src="{{ STATIC_URL }}scripts/spryMap-2.js"></script>
Update: I'm not sure why my answer was down voted. I've expanded my answer to provide more detailed instructions for fixing your static files.
The problem is your static files are not being served correctly - hence the 404 when you try to access the js file directly. As I mentioned above, the templates folder is not the right place for static files. You should configure your STATIC_ROOT
and STATIC_URL
settings. For example
#Make this the path to the folder where you will keep your static files
STATIC_ROOT = '/path/to/your/project/static'
#The absolute or relative URL which will serve your static files
STATIC_URL = '/static/' #translates to http://localhost:8000/static/, for example
and to get the django development server to serve your static files you will need the following in your urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
Upvotes: 1
Reputation: 736
I just checked this code and it seems to be working fine, perhaps try checking the script source path tag. I used
<script src="{{ STATIC_URL }}assets/js/spryMap-2.js"></script>
instead of
<script type="text/javascript" src="scripts/spryMap-2.js"></script>
and i got the attached output which is draggable:
Incase you are using Django make sure you mention the correct path in the settings file for the static path and put your sprymap.js file to that location, i guess this will sort the problem.
Upvotes: 0