how to include different js and css files when using block content using jinja

Viewed 1302

I am creating a web app using Django. I have a html template as follows:

<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

  {% block content %}
  {% endblock %}

</body>
</html>

I am using this template to create other html files using

{% extends 'document.html' %}

{% block content %} {% endblock %}

for the moment I am referring to all css and js files in the original document.html. how can I refer to different js and css files in the new templates only when necessary? thanks

2 Answers

Put this in your setting.py

STATIC_URL = '/static/'

STATIC_ROOT = os.path.dirname(BASE_DIR) + '/staticfiles/'

 STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 )

create static folder in created app and put there your css, js and access via this

<link href="{{ static('/app_name/css/reset.css') }}" rel="stylesheet">

<script type="text/javascript" src="{{ static('/app_name/js/jquery-1.11.3.js') }}"></script>

now it's working. I made just few changes

{% extends 'document.html' %}
{% load staticfiles %}

{% block content %}

now I was able to include the js or css files I need it in the newly created template as follow for example

<script src="{% static 'vendors/js/tableexport.min.js' %}"></script>
Related