14
votes

I am uisng python,To display data from json file to a page,i am getting the below errors

Failed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/script/jquery-1.9.1.min.js
Failed to load resource: the server responded with a status of 404 (NOT FOUND) http://localhost:8000/static/script/myscript.js

myscript.js file

$("#button").click(function(){
    $.getJSON("item.json",function(obj){
       $.each(obj,function(key,value){
          $("ul").append("<li>+value.item1+"</li>");
          $("ul").append("<li>+value.item2+"</li>");
          $("ul").append("<li>+value.item3+"</li>");
       });
    });
}); 

.json file is

{
"p1":{
      "item1":"apple",
      "item2":"orange",
      "item3":"banana",
      },
"p2":{
      "item1":"water",
      "item2":"milk",
      "item3":"alcohol",
     }
}

template is

<html>
    <head>
    <body>
    <ul></ul>
    <button></button>
    <script src="script/jquery-1.9.1,min.js" type="text/javascript"></script>
    <script src="script/myscript.js" type="text/javascript"></script>
    </body>
    </head>
</html>

1).js file is in my project folder and path also setted.

2).I am not doing any query in my views.py,as i am new to this i am confused with this.So any codings need to perform in the views.py for fetching the data from json.

3).Not able to sort out the above errors,please provide me the possible reason so that i can run this function.

Thanks

3
If that's exactly your code, you have an error here: ` $("ul").append("<li>+value.item1+"</li>");` , missing " after <li> (this might not be the solution to your problem, but at least your js won't crash)Daniel Ursu
I would suggest you to open those <script src= links directly in the browser and see what happens. I would expect 404 too, which would mean that the src paths are wrong.Akos K
@Daniel i corrected that even after i am getting the same errorMonk L
@akoskm i am getting the same error while checking the script src link in brower,so how to set path,i am mentioned the same file path as script src,is any thing different to mention.Please inform meMonk L
@MonkL no, the path should work equivalently when you paste it into the browser or retrieve it through a script tag. This behavior implies that the path under src is invalid and your script is placed elsewhere.Akos K

3 Answers

2
votes

you need to set STATICFILES_DIRS in settings.py but make sure your file tree is like below

RootDir
+--myproject
|  +--static
|  |  +--script
|  |     |--myscript.js
|  |     |--jquery-1.9.1.min.js
|  |--settings.py
+--myapp
|--manage.py

in settings.py add or replace

STATIC_URL = '/static/'

with

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]
STATIC_URL = '/static/'

another fix: replace comma , in <script src="script/jquery-1.9.1,min.js"

and

add double quote ", replace "<li>+value.item1+"</li>" with "<li>"+value.item1+"</li>"

0
votes

You have to set your STATIC_URL and STATIC_ROOT in your django settings file, prepend your js src path with {{ STATIC_URL }}, and tell django to serve static content when using the dev server (and/or configure your front server to serve it). It's all documented here: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/

NB : before version 1.3 it's MEDIA_URL and MEDIA_ROOT, cf the relevant doc.

0
votes

For the development server

  1. Make a static folder in the django root
  2. Add this to the STATIC_DIRS in settings.py ('assets','path to the static folder')
  3. Put the Resources in the respective folders in the static folder you had created earlier
  4. Then run python manage.py collectstatic. This will create an admin and an assets folder in the django root with the assets you have put
  5. In the Templates add {% load static %} at the top
  6. For the link use {% static 'assets/path_to_resources_as_added_in_the_static_folder' %}

This works for me