[英]How do I pass a variable from Django's render method's context param to a javascript script on the frontend?
For context here, I am experienced with JavaScript but new to Django.对于这里的上下文,我对 JavaScript 有经验,但对 Django 不熟悉。
Goal: Log the contents of a Django context variable in the browser's console using JavaScript.目标:使用 JavaScript 在浏览器的控制台中记录 Django 上下文变量的内容。 Specifically I'd like to log the contents of
context
in the Django render method I write below.具体来说,我想在下面写的 Django 渲染方法中记录
context
的内容。
In script.js
I have console.log("hey", foo)
which I expect to output the contents of a variable I pass into the Django render method.在
script.js
我有console.log("hey", foo)
我希望输出我传递给 Django 渲染方法的变量的内容。 It gives me an error: Uncaught ReferenceError: foo is not defined
它给了我一个错误:
Uncaught ReferenceError: foo is not defined
I found two StackOverflow threads that seem to answer my question re: how to do this correctly.我发现两个 StackOverflow 线程似乎回答了我的问题:如何正确地做到这一点。 Neither worked.
都没有工作。
In (1) How can I pass my context variables to a javascript file in Django?在 (1) 如何将我的上下文变量传递给 Django 中的 javascript 文件? , the answer from user Brian Neal claims I can deliver the Django variable into a javascript script at the top of the html file Django uses to render the page.
,来自用户 Brian Neal 的回答声称我可以将 Django 变量传递到 Django 用来呈现页面的 html 文件顶部的 javascript 脚本中。 Here is how I tried his answer:
这是我如何尝试他的回答:
Django's render method says... Django 的 render 方法说...
def page(request):
bar = "a"
doohickey = "b"
context = {'foo': bar,'baz': doohickey}
return render(request, 'myPage.html',context=context)
Then in myPage.html
I say:然后在
myPage.html
我说:
{% load static %}
{% block js %}
<script>var foo = {{ foo|safe }}; console.log("aliens");</script>
<script src="{% static 'js/script.js' %}" type="text/javascript" defer></script>
{% endblock %}
And script.js
says console.log("hey", foo)
而
script.js
说console.log("hey", foo)
But I get the error message written above.但是我收到上面写的错误消息。
In (2), How to use the context variables passed from Django in javascript?在(2)中, 如何在javascript中使用从Django传递过来的上下文变量? , a similar problem is presented and answered.
,提出并回答了类似的问题。 The ticked answer from user xyres says the same process will (seemingly) work for me.
来自用户 xyres 的勾选答案说相同的过程(似乎)对我有用。
Both threads say to use double {{ }} tags to pass the django variable to a JavaScript script.两个线程都说使用双 {{ }} 标签将 django 变量传递给 JavaScript 脚本。
However, I am clearly doing something wrong in myPage.html
, because "aliens" does not print to the console.但是,我显然在
myPage.html
做错了myPage.html
,因为“外星人”没有打印到控制台。 I've been through 8 threads and none of them tell me what to do.我已经经历了 8 个线程,但没有一个告诉我该怎么做。
I also found a blog Safely Including Data for JavaScript in a Django Template which claims using the answers I linked to on StackOverflow would create an opportunity for hackers.我还发现了一个博客,在 Django 模板中安全地包含 JavaScript 的数据,声称使用我在 StackOverflow 上链接的答案将为黑客创造机会。 So I don't want that.
所以我不想那样。
I also found an Edureka page with an answer to the same question I have and did this in myPage.html -- note the tag is now outside of the {% block js %}
part:我还发现了一个 Edureka 页面,其中包含对我所遇到的相同问题的回答,并在 myPage.html 中进行了此操作——请注意,该标签现在位于
{% block js %}
部分之外:
{% extends "base.html" %}
{% load static %}
<script>var foo = {{ foo|safe }}; console.log("aliens");</script>
{% block js %}
<script>var foo = {{ foo|safe }}; console.log("aliens");</script>
<script src="{% static 'js/script.js' %}" type="text/javascript" defer></script>
{% endblock %}
{% block body %}
No change though.虽然没有变化。
Another update:另一个更新:
When I write context = {"magic": "wizard"}
and pass that to the frontend, <script>console.log("aliens"); var something = {{ magic }}; console.log(something);</script>
当我编写
context = {"magic": "wizard"}
并将其传递给前端时, <script>console.log("aliens"); var something = {{ magic }}; console.log(something);</script>
<script>console.log("aliens"); var something = {{ magic }}; console.log(something);</script>
<script>console.log("aliens"); var something = {{ magic }}; console.log(something);</script>
gives me an error, Uncaught SyntaxError: Unexpected token '&'
. <script>console.log("aliens"); var something = {{ magic }}; console.log(something);</script>
给了我一个错误, Uncaught SyntaxError: Unexpected token '&'
。 Upon investigation, I'm closer than I've ever been to making it work.经过调查,我比以往任何时候都更接近让它发挥作用。 The code is embedded with bugs:
var something = "wizard";
代码中嵌入了错误:
var something = "wizard";
if the django variable is a string, quotes are needed.如果 django 变量是字符串,则需要引号。 so it would be like:
所以它会像:
var foo = "{{foo}}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.