【问题标题】:Django ClearableFileInput widget doesn't set FileField to NoneDjango ClearableFileInput 小部件未将 FileField 设置为 None
【发布时间】:2013-06-16 11:52:51
【问题描述】:

我的 Django 应用正在保存文章,并包含一个使用 ClearableFileInput 小部件的上传文件字段。当我将文章设置为发布时,它会调用任务来发送电子邮件。

它似乎工作,但有一个错误。当我编辑现有文章并标记“清除”复选框时,发送电子邮件任务失败。错误说 IOError: [Errno 21] Is a directory: 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()

【问题讨论】:

    标签: django forms email filefield


    【解决方案1】:

    我应该撤回这个问题,答案是我将 None 与 Empty 混淆了。

    我变了

    if article.docfile != None:  
    

    if article.docfile:
    

    世界上一切都很好。

    【讨论】:

    • 我得等到明天。到时候我会的。
    猜你喜欢
    • 2014-10-01
    • 2012-01-10
    • 2013-11-15
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多