Django 信号发射一次,接收两次——为什么?

发布于 2024-08-05 08:16:24 字数 1211 浏览 6 评论 0原文

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

# 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?)

I'm working with Django signals, but they seem to be received twice, even if emitted once. Here's the code I'm working with (it's a simple wrapper to use Uploadify with 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)

(I just noticed my signal is spelled wrong)

I'm sure you noticed the print statements in there. On the console, this is what it's showing:

(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 -

(also odd is why does Django report the page POST after the signals are fired?)

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

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

发布评论

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

评论(2

败给现实 2024-08-12 08:16:24

我以前也遇到过这种情况,这是由于您连接信号的模块被导入了两次。为了确保信号不会连接两次,您可以设置dispatch_uid:

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

UPDATE
它实际上记录在这里: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemissedtwiceforeachsave

This has happened to me before and it was due to the module where you are connecting the signal being imported twice. To make sure the signal isn't connected twice you can set the dispatch_uid:

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

UPDATE
It is actually documented here: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave

双马尾 2024-08-12 08:16:24

您可以检查函数中的“创建”参数,您正在与分别返回 True 和 False 的信号连接。仅当创建新对象时,Created 才会为 True。

def task_feedback_status_handler(sender, instance, created, **kwargs):
    if created:
        do something
post_save.connect(task_feedback_status_handler, sender=Feedback)

you can check "created" argument in your function that you are connecting with signal which returns True and False respectively. Created will be True only when a new object is created.

def task_feedback_status_handler(sender, instance, created, **kwargs):
    if created:
        do something
post_save.connect(task_feedback_status_handler, sender=Feedback)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文