【问题标题】:Difficulty uploading a file to a FileField, form isn't valid难以将文件上传到 FileField,表单无效
【发布时间】:2018-03-15 16:24:12
【问题描述】:

晕,我正在尝试使用文件字段上传文件。但我总是失败。当语句form.errors.as_data() 执行时,浏览器返回'tempfile'。我已经尝试从 django 文档和一些 django 参考资料中找到解决方案。但是,还是解决不了。 ;(

这是我的观点.py

def dataprocessing(request):
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)

    if form.is_valid():
       import pdb; pdb.set_trace()
       newdoc = Document(docfile=request.FILES['myfile'])
       newdoc.save()

       #Redirect to the dataprocessing after POST
       #return render(request, 'dataprocessing.html')
       return HttpResponse("success")
    else:
       return HttpResponse(form.errors.as_data())

else:
     import pdb; pdb.set_trace()
     form = DocumentForm() #A empty, unbound form

return render(request, 'dataprocessing.html', {'form': form})

models.py

class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')

forms.py

class DocumentForm(forms.Form):
tempfile = forms.FileField()

还有dataprocessing.html

<form method="post" enctype="multipart/form-data" action="{% url "dataprocessing" %}">

                    <div class="form-group">
                        <label for="up">Input Data</label> {% csrf_token %}
                        <input type="file" name=myfile class="filestyle" data-buttonName="btn-primary" data-buttonBefore="true" data-size="sm" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                            id="up">
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-block">Upload Data</button>
                        <button type="button" class="btn btn-primary btn-block">Download Template</button>
                    </div>
                </form>

【问题讨论】:

  • 请为您的问题选择一个更合适的标题。
  • 您的模板不是为tempfile 字段上传文件,而是为myfile 上传文件(那应该是一个字符串:&lt;input name="tempfile" .../&gt;
  • 尝试学习使用调试工具,以便您可以检查实际发布的内容,从而更容易解决此类问题。例如。在代码中使用 pdb 行设置断点,或使用 django-debugger 检查请求。
  • 谢谢@DanielRoseman
  • @dirkgroten yuup。我会尝试再次调试它。谢谢楼主

标签: django file-upload python-3.5 django-1.11


【解决方案1】:

像这样使用forms.ModelFormforms.Form 怎么样?

# forms.py
class DocumentForm(forms.Model):
    class Meta:
        model = Document
        fields = ['tempfile']

让你的views.py 像这样:

# views.py
def dataprocessing(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponse("success")
        else:
            return HttpResponse(form.errors.as_data())
    else:
        form = DocumentForm() #A empty, unbound form
    return render(request, 'dataprocessing.html', {'form': form})

这使得form 对象可以直接保存到您的模型中。

【讨论】:

  • 嗨,我正在尝试您的解决方案,并提出这个Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form DocumentForm needs updating. 所以这意味着我们必须创建至少 1 个字段?
  • 哦,对不起。我忘了。你可以简单地使用fields = ['tempfile']
  • 答案已更新!
  • 我认为问题出在他的 HTML 表单上,而不是表单后端。在这种情况下,使用ModelForm 确实更简单,也是推荐的方法,但并不能真正解决问题。
  • 如果您认为表单有问题,那么在您的 HTML 中使用{{ form.as_p }} 怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 2021-05-21
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多