0

I'm trying to add javascript static file in to my html. But i couldn't

<html>
<head>
    <title>SpaceX</title>
<script src="static/js/script.js" ></script>
</head>
<body>
<img src="static/img/img.jpg">
</body>
</html>

Image is working correctly but the JS is not

Url:

 from django.contrib import admin
    from django.urls import path, include
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('spacex/', include('spacex.urls'),
             )
    ]
    urlpatterns += staticfiles_urlpatterns()

Script

alert('abc') ;

Setting:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

And on powershell

[28/Dec/2019 19:12:20] "GET /spacex/ HTTP/1.1" 200 166
[28/Dec/2019 19:12:20] "GET /spacex/static/js/script.js HTTP/1.1" 304 0

Saying like that so what's the problem can you help me ?

1
  • Please don't link to images of code. Include all the code in the question itself Commented Dec 28, 2019 at 16:12

1 Answer 1

1

You need to use the staticfiles app template tags

{% load static %}
<script src="{% static "js/script.js" %}" ></script>
Sign up to request clarification or add additional context in comments.

4 Comments

Hello when i try this it says like that : Invalid block tag on line 7: 'import'. Did you forget to register or load this tag? Furthermore, i can implement css files withhout {% static 'css/styles.css'%} i just write this href="static/css/styles.css" and it works
Sorry I used "import" instead of "load". Try it with the updated answer?
Oh yeah it worked I thank you very much ! I'm trying to this stuff for 2 hours do you know any good documentation about django? i dont have enough reputation to vote up you :(
The tutorial from the Django docs is pretty useful docs.djangoproject.com/en/3.0/intro/tutorial01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.