Django 模板 - 将 Python 列表转换为 JavaScript 对象 [英] Django Template - Convert a Python list into a JavaScript object

查看:20
本文介绍了Django 模板 - 将 Python 列表转换为 JavaScript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 Django/Python 网站上工作.我有一个页面,我想在其中显示搜索结果表.结果列表照常传递给模板.

I am working on a Django / Python website. I have a page where I want to display a table of search results. The list of results is passed in to the template as normal.

我还想让 JavaScript 代码可以访问此对象列表.

I also want to make this list of objects accessible to the JavaScript code.

我的第一个解决方案是创建另一个返回 JSON 格式的视图.但是每个页面加载都需要调用两次查询.因此,我尝试仅使用 JSON 视图下载数据并使用 JavaScript 打印表格.

My first solution was just create another view that returned JSON format. But each page load required calling the query twice. So then I tried only downloading the data using the JSON view and printing the table using JavaScript.

但这也是不可取的,因为现在表示层已混合到 JavaScript 代码中.

But this is also not desirable as now the presentation layer is mixed into the JavaScript code.

有没有办法在页面呈现时从 Python 列表创建一个 JavaScript 对象?

Is there a way to create a JavaScript object from the Python list as the page is rendered?

推荐答案

解决方案

我创建了一个自定义模板过滤器,参见自定义模板标签和过滤器.

I created a custom template filter, see custom template tags and filters.

from django.core.serializers import serialize
from django.db.models.query import QuerySet
from django.utils import simplejson
from django.utils.safestring import mark_safe
from django.template import Library

register = Library()

def jsonify(object):
    if isinstance(object, QuerySet):
        return mark_safe(serialize('json', object))
    return mark_safe(simplejson.dumps(object))

register.filter('jsonify', jsonify)
jsonify.is_safe = True   

对 mark_safe 的调用很重要.否则 Django 会逃避它.

The calls to mark_safe are important. Otherwise Django will escape it.

在模板中:

//Without template filter (you'll need to serialise in the view)
var data = jQuery.parseJSON('{{ json_data|safe }}');
alert(data.length);

//Using the template filter    
var data2 = jQuery.parseJSON('{{ record_list|jsonify }}');
alert(data2.length);

注意模板标签周围的单引号.

Note the single quotes around the template tag.

尽管我的下一个问题是 - 它真的安全吗?

Although my next question would be - is it REALLY safe?

在上述模板标签的 django 1.8 中工作的更新版本,它还处理传递平面值列表,即.values_list('myfield', flat=True):

An updated version working in django 1.8 of the above template tag that also handles being passed a flat values list, ie. values_list('myfield', flat=True):

from django.core.serializers import serialize
from django.db.models.query import QuerySet, ValuesListQuerySet
from django.template import Library

import json

register = Library()

@register.filter( is_safe=True )
def jsonify(object):

    if isinstance(object, ValuesListQuerySet):
        return json.dumps(list(object))
    if isinstance(object, QuerySet):
        return serialize('json', object)
    return json.dumps(object)

这篇关于Django 模板 - 将 Python 列表转换为 JavaScript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆