[Answer]-Django javascript code is not running but seperatly it is running

1๐Ÿ‘

*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()
๐Ÿ‘คEnrico

0๐Ÿ‘

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:
Attached Output for the above problem

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.

๐Ÿ‘คSirius

Leave a comment