如何使Python不将L附加到longs或在Django模板中忽略 [英] how to get python to not append L to longs or ignore in django template

查看:78
本文介绍了如何使Python不将L附加到longs或在Django模板中忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以避免python在每次从数据库中出来时都在Ints后面加上"L"吗? (注意:我正在使用Mysql)

Is there a way to get around python appending an "L" to Ints short of casting every time they come out of the database? (Note: I'm using Mysql)

或者,有没有办法忽略Django模板中的L? (因此,我不断收到无效的格式错误,但我不想每次都进行列表理解/广播)

OR, is there a way to ignore the L in django templates? (I keep getting invalid formatting errors because of this, but I'd rather not do list comprehension/casting EVERY time)

例如我有一个以对象的pk为键的字典,并且在萤火虫中得到了以下内容:

e.g. I have a dict with the object's pk as the key and I get the following in firebug:

无效的属性ID 警报({183L:<投票:colleen:1最能形容您在任何衣服上的衣柜...

invalid property id alert({183L: <Vote: colleen: 1 on Which best describes your wardrobe on any g...

模型:Question对象,其他属性无关紧要,因为所讨论的属性是pk

Model: Question object, other attributes don't matter because the attribute in question is the pk

视图:我没有编写代码,也无法很好地遵循代码,因此无法发布创建变量的部分,但这是一个将Question pks作为键和Vote对象的字典作为值(有问题的代码来自 http://code.google.com/p/django-voting/wiki/RedditStyleVoting )

View: I didn't write the code and I can't follow it too well, so I can't post the section where the variable is being created, but it is a dict with Question pks as keys and Vote objects as values (code in question is from http://code.google.com/p/django-voting/wiki/RedditStyleVoting)

模板: 产生问题的{ 警报({{vote_dict}});正在触发错误

Template: {% votes_by_user user on questions as vote_dict %} to produce the dict in question alert({{vote_dict}}); is triggering the error

虽然在这种情况下,我只是想提醒我返回的字典,但是当将字典或数组传递给js函数(由于L导致函数调用失败)时,这对我来说是一个反复出现的问题.带给您问题背后的动力)

While in this particular case I'm just trying to alert the dict I got back, this has been a recurring problem for me when passing dicts or arrays into js functions where the function call fails because of the L. (Just to give you motivation behind the question)

推荐答案

Django在这里没有任何问题.但是,由于我们并不十分了解您要实现的目标,因此很难为您提供相关的解决方案.

There's nothing wrong with Django here. However, it's going to be difficult to provide you with a relevant solution as we don't really know what you're trying to achieve.

无论如何,调用{{ vote_dict }}会调用所说的 dict的 __str__方法,这是常见的{key_repr:value_repr}模式.

Anyway, calling {{ vote_dict }} will call said dict's __str__ method, which is the common {key_repr:value_repr} pattern.

如果要执行以下操作:

{% for key, value in vote_dict.items %}
    {{ key }} : {{ value }}
{% endfor %}

如果没有L,您将得到期望的结果.

You'd get what you expect, without the L's.

在旁注中,alert({{vote_dict}})几乎总是会引发JS错误:alert的参数应该是字符串.

On a sidenote, alert({{vote_dict}}) will almost always raise a JS error: alert's parameter is supposed to be a string.

如果您要实现的目标是将Django项目无缝地传递到JS函数中(list实例确实可以实现),则可以定义一个模板过滤器,该过滤器将返回您所需的内容.

If what you're trying to achieve is to pass Django items into JS funcitons seamlessly (which could indeed be possible with list instances), you could define a template filter that would return what you need.

对于列表(或您想要表示为列表的任何迭代),可以使用以下代码:

For a list (or any kind of iterable that you'd want to represent as a list), you could use the following:

def js_list(iterable):
    return '[%s]' % ', '.join(str(item) for item in iterable)

这篇关于如何使Python不将L附加到longs或在Django模板中忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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