# Javascript code cannot be run

**URL:** <https://forum.djangoproject.com/t/javascript-code-cannot-be-run/25423>\
**Category:** Mystery Errors\
**Created:** [November 17, 2023, 9:15pm UTC](https://forum.djangoproject.com/t/javascript-code-cannot-be-run/25423 "2023-11-17T21:15:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Groonir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/groonir/32/17158_2.png) [@Groonir](https://forum.djangoproject.com/u/Groonir)\
**Post date:** [November 17, 2023, 9:15pm UTC](https://forum.djangoproject.com/t/javascript-code-cannot-be-run/25423/1 "2023-11-17T21:15:32Z")

</div>

Hello  
It is my first django project so i am sorry for any glaring issues.

I have a basic app which has a templates folder which contains an html file and a javascript file. When I cd to the project and paste python manage.py runserver and open it in my browser (Firefox), i get this warning and javascript does not print anything in the console.

Error:

> Loading failed for the with source “[http://127.0.0.1:8000/index.js”](http://127.0.0.1:8000/index.js%E2%80%9D).

Here is the file structure (i didnt add the default files):

```auto
project
    app
        templates
            index.html
            index.js
    urls.py
    views.py
            

```

urls.py

```auto
from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
    path('index views.index')
] 

```

views.py

```auto
from django.shortcuts import render, HttpResponse

# Create your views here.
def home(request):
    return render(request, 'index.html',)

```

index.html

```auto

<!DOCTYPE html>
<html>

<script src='../index.js'></script>
<body>

<button onclick="myFunction()">Click me!</button>

</body>
</html>

```

index.js

```auto
console.log('hello world');
function myFunction() {
    alert("Hello from a static file!");
  }
  

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 17, 2023, 9:24pm UTC](https://forum.djangoproject.com/t/javascript-code-cannot-be-run/25423/2 "2023-11-17T21:24:20Z")

</div>

Your JavaScript files aren’t templates, they belong in your staticfiles directory. Also, the url would (usually) be constructed with the `static` tag.

See the work you would have done at [Writing your first Django app, part 6 | Django documentation | Django](https://docs.djangoproject.com/en/4.2/intro/tutorial06/) in the official Django tutorial, along with the docs referenced on that page.

If you haven’t worked your way through either the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) or the [Django Girls Tutorial](https://tutorial.djangogirls.org/en/), that really should be your starting point.

---

<div class="post-metadata">

**Author:** ![Groonir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/groonir/32/17158_2.png) [@Groonir](https://forum.djangoproject.com/u/Groonir)\
**Post date:** [November 17, 2023, 9:36pm UTC](https://forum.djangoproject.com/t/javascript-code-cannot-be-run/25423/3 "2023-11-17T21:36:05Z")

</div>

Thanks for that!  
I have been following a youtube tutorial and was stuck as i couldn’t figure out why my files would not load. I have used the static tag but i omitted it here as i thought it wasnt the culprit. I added it back to get

```auto
<script src="{% static 'myapp/index.js' %}" ></script>

```

instead of

```auto
<script src="{% static '/index.js' %}" ></script>

```

…which solved my issue!
