How to use JS in Django?

38 views
Skip to first unread message

Matheus Bon

unread,
Aug 16, 2022, 4:10:14 PM8/16/22
to Django users
Hello friends

I'm new to Django and I'm having trouble using JS.

I want to create a sidebar for my project, however, for some reason, I can't get it to 'move'.

I tried the traditional way, putting the <script></script> in the index.html document, but it didn't work.

I tried another way, using the static files, creating a 'static' folder and configuring it in my project.

Sebastian Jung

unread,
Aug 16, 2022, 4:14:03 PM8/16/22
to django...@googlegroups.com
Hey.

Inspect website with browser and then view in console there are errors and in code wether yoz see <script> block

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/174df993-3356-4f34-936c-af64ea32bec6n%40googlegroups.com.

Shada Bahassan

unread,
Aug 16, 2022, 4:27:24 PM8/16/22
to django...@googlegroups.com
Hi, 

The simple way for beginner that trying to use already template from any resources and the runserver if It is ok?, you have to change based on what do you want.

See the simple example below:

<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>

<p>JavaScript can change the content of an HTML element:</p>

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

<p id="demo">This is a demonstration.</p>

<script>
function myFunction() { 
  document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>

</body>
</html>


--

Matheus Bon

unread,
Aug 16, 2022, 4:52:21 PM8/16/22
to Django users
So I don't need to create a folder with only the files in JS and activate with {% load static %}

Interesting... I did it this way, how does it work, I probably missed something I didn't see, thank you very much.

Eugene Davis

unread,
Aug 16, 2022, 5:24:41 PM8/16/22
to django...@googlegroups.com
Add it to ur parent html file in the head tag, do u know anything about the jinja templating engine?

Reply all
Reply to author
Forward
0 new messages