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

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

问题描述

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



我还想让这个JavaScript代码可以访问的对象列表。



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



但是这也是不可取的,因为现在表达层被混合到JavaScript代码。



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

解决方案

解决方案



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

$ b从django.core.serializers导入serialize
从django.db.models.query导入
$ b

从django.utils导入simplejson的QuerySet 

from django.utils.safestring import mark_safe
from django.template import Library

register = Library()

def jsonify(object):
如果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将会退出它。



在模板中:

  /没有模板过滤器(您需要在视图中进行序列化)
var data = jQuery.parseJSON('{{json_data | safe}}');
alert(data.length);

//使用模板过滤器
var data2 = jQuery.parseJSON('{{record_list | jsonify}}');
alert(data2.length);

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



虽然我的下一个问题是 - 真的很安全吗?



更新



在上述模板标签的django 1.8中,也处理传递一个平值列表,即。 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)


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.

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

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.

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

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

解决方案

Solution

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   

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

In the template:

//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?

Update

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天全站免登陆