简体   繁体   English

带有动态javascript的Django 1.8.1加载模板文件

[英]Django 1.8.1 load template file with dynamic javascript

Can I load template file with dynamic javascript file. 我可以使用动态javascript文件加载模板文件吗? For example when I render a.html, I want to load a.js 例如,当我渲染a.html时,我想加载a.js

How can I do this? 我怎样才能做到这一点?

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: 如果您只想包含静态javascript文件,则可以采用与在HTML中加载其他任何javascript文件相同的方式进行操作:

<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: 但是,如果您要传递的是模板将加载的javascript文件,具体取决于某些后端输出,则可以执行以下操作:

#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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM