0
我想在我的crontab中運行自動通訊功能,但不管我嘗試什麼 - 我無法讓它工作。這樣做的正確方法是什麼? 這是我的crontab條目:在cron中的Django獨立運行
0 */2 * * * PYTHONPATH=/home/muntu/rails python2.6 /home/muntu/rails/project/newsletter.py
而且newsletter.py文件,該文件位於我的Django項目的頂層文件夾:
#! /usr/bin/env python
import sys
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
from django.core.management import setup_environ
from project import settings
setup_environ(settings)
from django.template.loader import get_template, render_to_string
from django.template import Context
from django.core.mail import EmailMultiAlternatives
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.conf import settings
from project.utilsFD.models import *
from django.http import HttpResponse, HttpResponseRedirect, Http404
def main(argv=None):
if argv is None:
argv = sys.argv
template_html = 'static/newsletter.html'
template_text = 'static/newsletter.txt'
newsletters = Newsletter.objects.filter(sent=False)
adr = NewsletterEmails.objects.all()
for a in adr:
for n in newsletters:
to = a.email
from_email = settings.DEFAULT_FROM_EMAIL
subject = _(u"Newsletter - Method #1")
text_content = render_to_string(template_text, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})
html_content = render_to_string(template_html, {"title": n.title,"text": n.text, 'date': n.data, 'email': to})
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.content_subtype = "html"
msg.send()
n.sent = True
n.save()
if __name__ == '__main__':
main()
我在做什麼錯?該函數本身不工作時爲Django應用程序運行的任何問題,但是當我試圖從控制檯運行它,它給了我:
Traceback (most recent call last):
File "newsletter.py", line 7, in <module>
from project import settings
ImportError: No module named project
而且它不會從cron在所有的工作。
是否存在'/ home/muntu/rails/project/__ init __。py'? – unutbu 2010-07-25 02:24:06
你在'project'文件夾中有'__init __。py'文件嗎? – 2010-07-25 02:45:52
當然可以。我想沒有任何django項目沒有辦法運行沒有它:) – muntu 2010-07-25 10:28:39