0

This is i am doing to send mail once a record is updated in the database.I had defined the receivers in separate file called listeners.py to receive the signals.

signals.py

import django.dispatch

send_email_to = django.dispatch.Signal()

listeners.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)

views.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))

I am getting an error message as ahown below.

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

Can any one see what would be the problem.

Thanks

user2086641
  • 4,331
  • 13
  • 56
  • 96

1 Answers1

0

Because you defined send_update in your views that has no value. That's why the error say you need to define the send_update.

But why you put the post_save.connect(send_update, sender=Book) in your views? You must remove that. It is already understood in your listeners.py. So whatever action you make using Book model, that model will send a request to that signal.

catherine
  • 22,492
  • 12
  • 61
  • 85
  • you modify my answer, what are you trying to achieve? Or do you want to learn on how to use signals? – catherine Mar 24 '13 at 14:37
  • i want to learn more about signals,but in django doc the examples what they given was not understandable to me – user2086641 Mar 25 '13 at 04:13
  • Ok I understand but back the codes in the original so that it will work. I have a blog, I post there on how to use Django, just a basic codes. I'm planning also to put about signal but I did not publish it yet. http://catherinetenajeros.blogspot.com/ – catherine Mar 25 '13 at 04:17
  • Catherine,can you please check this SO question http://stackoverflow.com/questions/17652075/showing-the-checked-item-alone-without-check-box – user2086641 Jul 15 '13 at 13:43