0

I am getting some error while including JavaScript files in templates using Django.

Error:

TemplateSyntaxError at /
Invalid block tag on line 6: 'static'. Did you forget to register or load this tag?
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.11.2
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid block tag on line 6: 'static'. Did you forget to register or load this tag?
Exception Location: C:\Python34\lib\site-packages\django\template\base.py in invalid_block_tag, line 571
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.4

settings.py:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

PROJECT_DIR = os.path.dirname(__file__)

""" Internationalization """

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


""" Static files (CSS, JavaScript, Images) """

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

urls.py:

"""Neuclear plant URL Configuration"""
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('plant.urls')),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

base.html:

 <!DOCTYPE html>
<html lang="en">
  <head>
<meta charset="utf-8">
{ % load static %}
<script type="text/javascript" src="{% static 'js/jquery.js' %}"></script>
  </head>
  <body>
    <header>
      <h1>Nuclear Reactor</h1>
      {% if count > 0  %}
      <b>Hi, {{ user.username }}</b>
        <a href="{% url 'home' %}">Home</a>
        <a href="{% url 'view_reactor' %}">View Reactor status</a>
        <a href="{% url 'logout' %}">logout</a>
      {% else %}
        <a href="{% url 'login' %}">login</a> / <a href="{% url 'signup' %}">signup</a>
      {% endif %}
      <hr>
    </header>
    <main>
      {% block content %}
      {% endblock %}
    </main>
  </body>
</html>

In the above template file i am getting the error. Here I need to include the js file in this template. I have also static/js/ folder inside my project directory.

4
  • try putting load static on the top of your file Commented Aug 29, 2017 at 17:43
  • 1
    { % load static %} there's a space between { and % Commented Aug 29, 2017 at 17:56
  • @dirkgroten: thanks as per you it worked. Commented Aug 29, 2017 at 17:58
  • @marni gave the correct answer, pls mark it as correct. Commented Aug 29, 2017 at 17:59

2 Answers 2

3

Include {% load static %} on the top of your template

Sign up to request clarification or add additional context in comments.

3 Comments

I put it before <!DOCTYPE html> also but same error.
Try it: http://127.0.0.1:8000/static/js/jquery.js if you will get js - all with settings are ok. Can you paste here the code where you make loading?
it's {% not { %
-1

Add urlpatterns += staticfiles_urlpatterns() in urls.py

Comments

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.