0

这是我在数据库中更新记录后发送邮件的目的。我已经在名为 listeners.py 的单独文件中定义了接收器来接收信号。

信号.py

import django.dispatch

send_email_to = django.dispatch.Signal()

听众.py

@receiver(send_mail_to)
def send_update(sender, instance, created, **kwargs):
    if instance.author_name:
        message = "Book details has been updated"
        subject = "Book updates"
        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,[instance.email,])

post_save.connect(send_update, sender=Book)

视图.py

def addbook(request):      

    form = BookForm

    if request.POST:
        form = BookForm(request.POST)

        if form.is_valid():
            cd = form.cleaned_data
            form.save()
            post_save.connect(send_update, sender=Book)     
            return redirect('/index/')
    return render_to_response('addbook.html',{ 'form':form },context_instance=RequestContext(request))

我收到如下错误消息。

NameError at /addbook/
global name 'send_update' is not defined
Request Method: POST
Request URL:    http://localhost:8000/addbook/
Django Version: 1.4.3
Exception Type: NameError
Exception Value:    
global name 'send_update' is not defined
Exception Location: /root/Samples/DemoApp/DemoApp/views.py in addbook, line 50
Python Executable:  /usr/bin/python
Python Version: 2.7.0
Python Path:    
['/root/Samples/DemoApp',
 '/usr/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg',
 '/usr/lib/python27.zip',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib/python2.7/site-packages/PIL',
 '/usr/lib/python2.7/site-packages/gst-0.10',
 '/usr/lib/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/usr/lib/python2.7/site-packages/webkit-1.0']
Server time:    Sat, 23 Mar 2013 19:05:01 +0500

任何人都可以看到会是什么问题。

谢谢

4

1 回答 1

0

因为您send_update在视图中定义了没有价值的东西。这就是为什么错误说您需要定义 send_update。

但是为什么你把它post_save.connect(send_update, sender=Book)放在你的观点中呢?你必须删除它。在你的 listeners.py 中已经理解了。因此,无论您使用 Book 模型执行什么操作,该模型都会向该信号发送请求。

于 2013-03-24T14:33:21.743 回答