我正在使用 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?)