[Fixed]-Django 1.10 โ€“ Use django.shortcuts.render to generate a webpage with variables which includes a javascript as parameter

1๐Ÿ‘

โœ…

You should put that script in a separate file and then pass the file name to the template instead.

Put your script in a js file, say my_script.js:

window.lpTag=window.lpTag||{};if(typeof window.lpTag._tagCount==='undefined') ...

Then in your view:

def site(request):
    return render(request, "sites/site.html", {'date': strftime("%A, %B %d %Y"),
                                               'site': '123456',
                                               'title': 'Test',
                                               'script': 'my_script.js'})

Then in your HTML:

<head>
    <script type="text/javascript" src="{{ script }}"></script>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="{% static 'styles.css' %}"/>
    <title>{{ title }}</title>
</head>
๐Ÿ‘คthanksd

Leave a comment