2016-04-28 28 views
1

我聆听了线程,但我不知道如何使用它以及放置函数的位置。我有一个应用程序邮件发送电子邮件,我想在运行服务器时控制温度,并在温度高于最高温度时发送电子邮件。 邮件/ views.py如何在Django中使用线程

from django.core.mail import send_mail, BadHeaderError 
from django.http import HttpResponse, HttpResponseRedirect 


def index(request): 
    subject = request.POST.get('subject', 'subject') 
    message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ') 
    from_email = request.POST.get('from_email', '***********@gmail.com') 
    if subject and message and from_email: 
     try: 
      send_mail(subject, message, from_email, ['*********@gmail.com']) 
      return HttpResponse('templates/mail.html') 
     except BadHeaderError: 
      return HttpResponse('Invalid header found.') 
     return HttpResponseRedirect('mail') 
    else: 

     return HttpResponse('Make sure all fields are entered and valid.') 
+0

如果我们想在后台运行的东西,看看使用芹菜 – Johan

+0

我理解,但我的老师迫使我使用线程,但我仍然不知道如何使用它还没有找到任何教程谈论 – fraulein

回答

0

您需要使用这个后台工作,线程是不是在你的情况有所帮助。
这里是库的列表,你可以使用:
https://stackoverflow.com/a/36386133/3896300

+0

我明白,但我的老师不得不使用线程,但我仍然不知道如何使用它没有发现任何教程谈论 – fraulein

+0

也许你需要找到其他老师? :) – ahmed

+0

hhh我不这么认为:) – fraulein