【问题标题】:Django FileField upload is not working for meDjango FileField 上传对我不起作用
【发布时间】:2012-08-12 14:37:01
【问题描述】:

我一直在挠头 FileField。 FileField 需要单独的进程吗?

虽然我的网址已保存..但我的文件没有上传...我做错了什么?

这是我的models.py ...

class OpLink(models.Model):
    user = models.ForeignKey(User)
    file = models.FileField(blank=True, null=True, upload_to="uploads")
    url = models.URLField(blank=True, null=True)

我的表单.py

class OpLinkForm(ModelForm):
    class Meta:
        model = OpLink
        exclude = ('user')

我的意见.py

oplinkform = oplinkform(request.POST)
                oplink = oplinkform.save(commit=False)
                oplink.user = user
                oplink.save()

和我的 html 来处理它。

<div class="span5">
                            {{ oplinkform.url|add_class:"span4"|attr:"Placeholder:URL for the item" }}
                            <br><h4>OR</h4><br>
                            {{ oplinkform.file|add_class:"input-file" }}
                            <br />
                            <input class='btn btn-primary btn-large' type="submit" value='Post' name='action'>
</div>

【问题讨论】:

  • 好问题 - 有同样的问题,完全忘记了我昨天在学习 FileField 时才读到这个。谢谢你的提问。

标签: django django-models django-forms django-views


【解决方案1】:

创建表单时需要包含文件

oplinkform = oplinkform(request.POST, request.FILES)

还要确保您的表单具有正确的编码类型

<form enctype="multipart/form-data"></form>

【讨论】:

  • 它如此简单,但错误地丢失了。但是 Mikael 你节省了我的时间,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2021-05-21
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
相关资源
最近更新 更多