gpt4 book ai didi

Django 信号发出一次,收到两次——为什么?

转载 作者:行者123 更新时间:2023-12-03 14:03:42 25 4
gpt4 key购买 nike

我正在使用 Django 信号,但它们似乎被接收了两次,即使发出一次。这是我正在使用的代码(这是一个将 Uploadify 与 Django 一起使用的简单包装器)...

# Signal-emitting code... emits whenever a file upload is received
# ----------------------------------------------------------------
upload_recieved = django.dispatch.Signal(providing_args=['data'])

def upload(request, *args, **kwargs):
if request.method == 'POST':
if request.FILES:
print 'sending signal'
upload_recieved.send(sender='uploadify', data=request.FILES['Filedata'])
return HttpResponse('True')

# Signal-receiving code...
# ----------------------------------------------------------------
def upload_received_handler(sender, data, **kwargs):
print 'upload received handler'

print 'connecting signal'
upload_recieved.connect(upload_received_handler)

(我刚刚注意到我的信号拼写错误)

我敢肯定你注意到了那里的打印语句。在控制台上,这就是它所显示的内容:
(server starts)
connecting signal

...

sending signal
upload received handler
upload received handler # << == where is this 2nd one coming from?
127.0.0.1 - - [25/Sep/2009 07:28:22] "POST /uploadify/upload/ HTTP/1.1" 200 -

(同样奇怪的是为什么 Django 会在信号触发后报告页面 POST?)

最佳答案

这发生在我之前,这是由于您连接信号的模块被导入两次。为了确保信号没有连接两次,您可以设置 dispatch_uid:

upload_recieved.connect(upload_received_handler, dispatch_uid="some.unique.string.id")

更新
它实际上记录在这里: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave

关于Django 信号发出一次,收到两次——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1478630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com