将对象从 Django 传递到 Javascript DOM
我正在尝试使用 javascript 将查询集从 Django 传递到模板。
我尝试了不同的方法来解决这个问题:
1。 ,Javascript 在尝试解析对象时变得一团糟
正常方法 - 由于命名法 [ > Object:ID <, > Object:ID <,... ] Django 视图
django_list = list(Some_Object.objects.all())
模板 HTML + JS
<script type="text/javascript" >
var js_list = {{django_list}};
</script>
2. JSON 方法 - Django 无法将对象列表转换为 json 字符串 不是 JSON 可序列化
Django 视图
django_list = list(Some_Object.objects.all())
json_list = simplejson.dumps(django_list)
模板 HTML + JS
<script type="text/javascript" >
var js_list = {{json_list}};
</script>
所以,我在这里需要一些帮助:)
有人有什么建议/解决方案吗?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
同样的问题,“更好”(更新)答案:Django Queryset to dict for use in json
vashishtha-jogi:
这个答案在我的谷歌搜索中经常出现,并且有很多观点,更新它并避免其他人挖掘这样的内容似乎是个好主意。假设使用 Django 1.5。
Same Question, "Better"(more recent) answer: Django Queryset to dict for use in json
Answer by vashishtha-jogi:
This is answer came up so often in my google searches and has so many views, that it seems like a good idea to update it and save anyone else from digging through SO. Assumes
Django 1.5
.好的,我找到了解决方案!
主要是因为没有引用结果。当 Javascript 尝试解析该对象时,该对象未被识别为字符串。
所以,第一步是:
更改为:
之后我意识到 Django 正在转义字符,所以我必须像这样替换它们:
注意:我尝试使用此方法避免从 Django 转义字符:
但这并不不起作用,因为它与引号混淆了。
最后,我找到了一种方法来避免在将其发送到 Javascript 之前转换为 JSON 的后端逻辑:
我确信这可以改进,我把这个告诉你;)
感谢您的回答
希望它对其他人有帮助!
Ok, I found the solution!
Mostly it was because of not quoting the results. When Javascript was trying to parse the object this wasn't recognized as string.
So, first step is:
changed to:
After this I realized that Django was escaping characters so I had to replace them like this:
Note: I tried to avoid escaping characters from Django using this:
But this doesn't work because it gets confused with the quotes.
Finally I found a way to avoid the logic on the backend of converting to JSON before sending it to Javascript:
I'm sure this can be improved, I let this to you ;)
Thanks for your answers
Hope it helps to someone else!
Django 查询集可通过 JSON 序列化。某些字段类型(显然是日期)无法序列化。日期对象的解决方法发布在 关于 JSON 和 Python 的另一个问题。
我建议直接在 JavaScript 本身中创建字典。给定这样的模型:
我会在模板中执行类似的操作:
如果您的问题措辞可能有点糟糕并且不计划在
Django querysets are serializable by JSON. Some field types (such as date, apparently), can't be serialized at is. A workaround for date objects is posted in another question on JSON and Python.
I would recommend creating dictionaries directly in the JavaScript itself. Given models like this:
I'd do something like this in the template:
If you maybe worded the question a little poorly and aren't planning on inserting code in a
<script>
tag and actually need JSON for some reason, I'd simply do a loop in the view and create a list ofdict
s, which JSON has no problem serializing, and JavaScript no problem in understanding.编辑:请不要使用此方法,请参阅@agconti 的答案。
使用 escapejs 过滤器: https://docs.djangoproject.com/ en/1.4/ref/templates/builtins/#escapejs
转储列表的示例:
EDIT: please don't use this method, see @agconti's answer.
Use the escapejs filter: https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#escapejs
Example of dumping a list:
从 Django 2.1 开始,就有 json-script 模板标签。来自文档:
Since Django 2.1 there is the json-script template tag. From the docs:
您可以使用 安全 和 escapejs Django 中的内置过滤器。
You can use the combination of safe and escapejs built-in filter in Django.
您的问题是,正如经常发生的那样,您的要求没有明确说明。您到底希望 JSON 是什么样子?你说你想“序列化查询集”,但以什么格式呢?您想要每个模型实例的所有字段、一个选择还是只是 unicode 表示形式?当你回答了这个问题后,你就会知道如何解决你的问题。
例如,一种方法可能是使用
values
queryset 方法输出每个实例的字段字典,并将其序列化(您需要首先将其转换为列表):Your problem is that, as so often, your requirements are under-specified. What exactly do you want the JSON to look like? You say you want to "serialize the queryset", but in what format? Do you want all the fields from each model instance, a selection, or just the unicode representation? When you've answered that question, you'll know how to solve your problem.
One approach, for example, might be to use the
values
queryset method to output a dictionary of fields for each instance, and serialize that (you need to convert it to a list first):您必须将字符串标记为安全,以确保它不会被转义。
在我的一个项目中,我像这样使用它:
在模板中
,但您可能更喜欢仅添加 mark_safe 或在模板中使用 |safe 以避免所有
>
内容如果问题是处理复杂的 python 对象,你可能必须像这样处理你的处理程序: Python 和 JavaScript 之间的 JSON 日期时间
You have to mark the string as safe to be sure it's not escaped.
in one of my project I use it like this:
and in the template
but you may prefer to just add mark_safe or use |safe in the template to avoid all
>
stuffIf the problem is for handling complex python object you may have to do your handler like this: JSON datetime between Python and JavaScript
Django 为您在此处尝试执行的场景提供内置帮助。它是这样的:
你的视图中有一个 python 序列、列表、字典等,我们称之为 py_object 。一种方法是在将其传递给渲染引擎之前对其进行 jsonify。
然后稍后像这样使用...
在您的模板中,然后使用
safe
过滤器将已经 json 化的对象从 python 导入到 javascript 中,就像这样...我希望这应该可以解决您的问题。
Django offers in-built help for the very scenario you are trying to do here. It goes something like this:
You have a python sequence, list, dictionary, etc in your view, let's call it
py_object
. One approach is to jsonify it before passing it to the rendering engine.Then later on use like this...
In you template, then use the
safe
filter to import the already jsonized object from python into javascript, like this...That should solve your problem i hope.
任何一个;
使用
{{ django_list }}
读取对象,然后删除不需要的字符或执行;
either;
read object using
{{ django_list }}
and then remove unwanted charactersor do;
综合答案(我的环境:Django 2.0)
在views.py中
在模板中
Consolidated answer (my env: Django 2.0)
In views.py
In template
让我发送整个查询集(同时保留字段名称;发送
object
而不是list
)。我使用了以下内容,仅在模板中使用
safe
标签For me to send the whole QuerySet (while preserving the fields names; sending
object
notlist
). I used the followingand just use
safe
tag in the templatedjango 2.1 的注释
我发现这在 django 文档中有点令人困惑,所以简单地解释了一点简单的方法。
使用它
我们通常会根据我们的需要 或循环它。但是,如果我们使用以下过滤器,
我们可以安全地将此值输出为 JSON
我们的数据已添加为 JSON,包装在脚本标记中,我们可以看到数据。我们现在可以通过在 JavaScript 中查找来使用该值,如下所示:
NOTE for django 2.1
i found this a little confusing on django documentation so simply explaining a little bit easy way.
we would normally use this like
or loop over it depending on what we needed. But if we use the following filter,
we can safely output this value as JSON
Our data has been added as JSON, wrapped in a script tag, and we can see the data. We can now use this value by looking it up in JavaScript like so:
还要小心确保从 Django 正确输出 JSON 数据,否则前端的所有试验都将浪费时间。就我而言,我无法使用 JsonResponse 作为渲染函数的一部分,因此我执行了以下操作:
在前端:
Be careful on also making sure that you output JSON data correctly from Django, otherwise all trials on the frontend side will be a waste of time. In my case I could not use JsonResponse as part of the render function so I did the following:
And on the frontend: