我正在制作一个待办事项应用程序其中一个功能是发送电子邮件告诉用户他的任务已经过期.
部分视图。py文件:
from .models import Todo
from django.utils import timezone
from .signals import overdue
def index(request):
...
overdue_tasks = Todo.objects.filter(time__lte=timezone.now())
if overdue_tasks:
overdue.send_robust(sender=Todo)
...
return render(request, 'todo/index.html', {'overdue_tasks' : overdue_tasks })
部分信号。py文件:
from django.dispatch import receiver, Signal
from .models import Todo
from django.core.mail import send_mail
overdue = Signal()
user_email = request.user.email
@receiver(overdue)
def my_handler(sender, **kwargs):
send_mail('Subject', 'Your task was Overdue !', 'myemail@gmail.com', 'user_email', fail_silently=False)'
引发异常:
"NameError: name 'request is not defined.">
您试图通过request.user.email
访问请求而没有请求访问,您可以在index
视图上访问用户电子邮件,将电子邮件作为**kwarg传递并访问my_handler
def index(request):
...
overdue_tasks = Todo.objects.filter(time__lte=timezone.now())
if overdue_tasks:
overdue.send_robust(sender=Todo, email=request.user.email)
...
return render(request, 'todo/index.html', {'overdue_tasks' : overdue_tasks })
@receiver(overdue)
def my_handler(sender, **kwargs):
email = kwargs.get('email')
send_mail(foo)