kbrk
kbrk

Reputation: 660

Django 1.8.1 load template file with dynamic javascript

Can I load template file with dynamic javascript file. For example when I render a.html, I want to load a.js

How can I do this?

Upvotes: 0

Views: 847

Answers (1)

JSchwerberg
JSchwerberg

Reputation: 148

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>

Upvotes: 1

Related Questions