I want to pass the argument to the javascript via the django view.
I always separate the js code and html code into two files, index.html and index.js.
Here is the django view
def index(request):
template = loader.get_template('index.html')
context = RequestContext(request,{'name':'guest'})
return HttpResponse(template.render(context))
I have the index.html like this
Access the argument name in index.html is ok.
<!DOCTYPE html>
<meta charset="utf-8">
{% load staticfiles %}
<script>
var name='{{ name }}'
console.log(name) //guest
</script>
<script src="{% static "js/index.js" %}"></script>
</body>
But in the external index.js, I can not access the argument.
var name ='{{ name }}'
console.log(name) //'{{name}}'
How can I pass the argument or variable to the external javascript?
Thank you!