[Answer]-Django 1.8.1 load template file with dynamic javascript

1๐Ÿ‘

โœ…

If you just want to include a static javascript file, you can do it the same way that you load any other javascript file in HTML:

<script src="jquery-1.11.2.min.js"></script>

However, if what you want to do is pass in a javascript file that the template will load, depending on some backend output, something like this will work:

#views.py
from django.shortcuts import render
from django.template import RequestContext

def my_view(request):
    # Logic here...
    js_file = "jquery-1.11.2.min.js"
    render_to_response('template.html', 
                       context_instance=RequestContext(request,{
                                        "js_file": js_file
                                         }))


#template.html
<script src="{{ js_file }}"></script>
๐Ÿ‘คJSchwerberg

Leave a comment