我的Django应用程序正在保存文章,并包含一个使用ClearableFileInput小部件的上传文件字段当我设置要发布的文章时,它正在调用发送电子邮件的任务。
它似乎有效,但有一个错误。当我编辑现有文章并标记“清除”复选框时,发送电子邮件任务将失败。错误说IOError:[Errno 21]是一个目录:u'/ path / to / my / static-dir'
在我的edit_article视图中,我尝试了这个逻辑:
def edit_article(request, slug):
article = get_object_or_404(Article, slug=slug)
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES, instance=article)
if form.is_valid():
article = form.save()
msg = "Article updated successfully"
messages.success(request, msg, fail_silently=True)
if article.is_published and article.publish_date <= datetime.datetime.today():
subject = article.title
email_text = article.email_text
story_text = article.text
if article.docfile != None:
attachment = article.docfile
send_published_article.delay(request.user.email,
subject,
email_text,
story_text,
attachment)
else:
send_published_article.delay(request.user.email,
subject,
email_text,
story_text)
msg = "Article published successfully"
messages.success(request, msg, fail_silently=True)
return redirect(article)
else:
form = ArticleForm(instance=article)
return render_to_response('story/article_form.html',
{
'form': form,
'article': article,
},
context_instance=RequestContext(request))
违规行可能是
if article.docfile != None:
但我应该用什么来检查文件是否已被清除? Django的ClearableFileInput小部件实际上将我的文件字段值设置为什么,所以我可以测试是否有要附加的文件?
这是我的tasks.py:
class EMail(object):
"""
A wrapper around Django's EmailMultiAlternatives
that renders txt and html templates.
Example Usage:
>>> email = Email(to='oz@example.com', subject='A great non-spammy email!')
>>> ctx = {'username': 'Oz Katz'}
>>> email.text('templates/email.txt', ctx)
>>> email.html('templates/email.html', ctx) # Optional
>>> email.send()
>>>
"""
def __init__(self, subject, to, cc, bcc):
self.subject = subject
self.to = to
self.cc = cc
self.bcc = bcc
self._html = None
self._text = None
self._attachment = None
def _render(self, template, context):
return render_to_string(template, context)
def html(self, template, context):
self._html = self._render(template, context)
def text(self, template, context):
self._text = self._render(template, context)
def add_attachment(self, attachment):
self._attachment = default_storage.open(attachment.name, 'r')
def send(self, from_addr=None, fail_silently=False):
if isinstance(self.to, basestring):
self.to = [self.to]
if not from_addr:
from_addr = getattr(settings, 'DEFAULT_FROM_EMAIL')
msg = EmailMultiAlternatives(
self.subject,
self._text,
from_addr,
to=self.to,
cc=self.cc,
bcc=self.bcc
)
if self._html:
msg.attach_alternative(self._html, 'text/html')
if self._attachment:
msg.attach(self._attachment.name, self._attachment.read())
msg.send()
@task(name='send-email')
def send_published_article(sender, subject, email_text, story_text, attachment=None):
"""
Task for emailing published articles.
Runs when an article is saved and is_published==True
"""
recipients = []
reporters = []
for profile in UserProfile.objects.all():
if profile.user_type == 'Client':
recipients.append(profile.user.email)
if profile.user_type == 'Reporter':
reporters.append(profile.user.email)
email = EMail(subject, to='nns.aroberts@gmail.com', cc=reporters, bcc=recipients)
ctx = {'story_text': story_text, 'email_text': email_text}
email.text('../templates/templated_email/emaila.txt', ctx)
email.html('../templates/templated_email/emaila.html', ctx) # Optional
if attachment != None:
email.add_attachment(attachment) # Optional
email.send()
答案 0 :(得分:0)
我应该撤回这个问题,答案是我将“无”与“空”混淆。
我改变了
if article.docfile != None:
到
if article.docfile:
所有人都对这个世界是正确的。