python random.random() 导致“”模块“ 对象不可调用” 当在自定义模板标签中使用时

发布于 2024-07-20 06:00:46 字数 972 浏览 4 评论 0原文

如果我从命令行启动 python 并输入:

import random
print "Random: " + str(random.random())

它会打印一个随机数(预期的,非常好)。

如果我将上述两行包含在我的 django 应用程序的 models.py 中并使用 runserver 启动我的 django 应用程序,我会在命令行上得到输出,显示一个随机数(太棒了!)

如果我采用自定义标签,则效果非常好,否则,但我将其

import random
print "Random: " + str(random.random())

作为自定义标记的 .py 文件的前两行,每当我尝试打开使用该自定义标记的模板时都会收到错误:

TypeError at /help/
'module' object is not callable

请记住,如果我删除这两行,我的自定义标记的行为与预期一致,并且不会引发错误。 不幸的是,我的模板标签内需要一些随机行为。

问题是,如果在自定义标签中我这样做:

import random

在自定义模板标签上,它会导入,

<module 'django.templatetags.random' from '[snip path]'> 

而不是

<module 'random' from 'C:\\Program Files\\Python26\\lib\\random.pyc'>

像通常从其他地方导入一样

Django 模板库有一个名为随机的过滤器,并且不知何故它的优先级高于系统的随机。

任何人都可以推荐如何显式导入正确的 python random 吗?

If I start python from the command line and type:

import random
print "Random: " + str(random.random())

It prints me a random number (Expected, excellent).

If I include the above-two lines in my django application's models.py and start my django app with runserver I get the output on the command line showing me a random number (Great!)

If I take a custom tag which works perfectly fine otherwise, but I include

import random
print "Random: " + str(random.random())

as the first 2 lines of the custom tag's .py file, I get an error whenever I try to open up a template which uses that custom tag:

TypeError at /help/
'module' object is not callable

Please keep in mind that if I get rid of these two lines, my custom tag behaves as otherwise expected and no error is thrown. Unfortunately, I need some random behavior inside of my template tag.

The problem is if in a custom tag I do:

import random

on a custom template tag, it imports

<module 'django.templatetags.random' from '[snip path]'> 

and not

<module 'random' from 'C:\\Program Files\\Python26\\lib\\random.pyc'>

as is normally imported from everywhere else

Django template library has a filter called random, and somehow it is getting priority above the system's random.

Can anyone recommend how to explicitly import the proper python random?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-07-27 06:00:46

答案是……奇怪。

当我最初编写自定义标签时,我将其称为 random.py。 我很快意识到这个名字可能不好,并将其重命名为 randomchoice.py 并删除了我的 random.py 文件。 Python 保留了编译后的 random.pyc 文件,每当我导入 random 时它都会被加载。 我删除了 random.pyc 文件,问题就消失了。

The answer is ... strange.

When I originally wrote my custom tag, I called it random.py. I quickly realized that this name may not be good and renamed it randomchoice.py and deleted my random.py file. Python kept the compiled random.pyc file around, and it was getting loaded whenever I did import random. I removed my random.pyc file, and the problem went away.

|煩躁 2024-07-27 06:00:46

是的,这种错误很容易发生。 基本上不要将任何文件名或您创建的任何内容命名为与您将要使用的任何可能的 python 模块相同的名称。

Yes, this kind of error is pretty easy. Basically don't name any of your filenames or anything you create with the same names as any likely python modules you are going to use.

慵挽 2024-07-27 06:00:46

自从我修改 Django 以来已经有一段时间了,但是如果 random.random 是一个“模块”,那么尝试 random.random.random() 。 或者也许只是尝试 random()。 你只是不知道幕后发生了什么样的黑客行为。

编辑

试试这个:

sys.path = [r"C:\Program Files\Python26\lib\"] + sys.path
import random
sys.path.pop(0)

Its been a while since I tinkered around with Django, but if random.random is a "module", then try random.random.random(). Or maybe just try random(). You just don't know what kind of hackery goes on behind the scenes.

Edit

Try this:

sys.path = [r"C:\Program Files\Python26\lib\"] + sys.path
import random
sys.path.pop(0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文