【问题标题】:Django can't upload multiple files and request.FILES seems emptyDjango 无法上传多个文件并且 request.FILES 似乎为空
【发布时间】:2019-01-26 02:19:13
【问题描述】:

我正在使用 django 2.0 和 python 3.6,我已经尝试了几乎所有方法来从 request.FILES 获取值,但似乎没有任何效果,我认为我遗漏了一些非常简单的东西但我找不到它.

models.py

class Imagen(models.Model):
imagen = models.FileField(max_length=200, blank=True, upload_to='%Y/%m/%d/')

views.py

def prop_add(request):
  if request.method == 'POST':
    propiedad_instance = Propiedad(tipo = request.POST.get('tipo'), tamano=request.POST.get('tamano'), habitaciones = request.POST.get('habitaciones'), banos = request.POST.get('banos'), descripcion= request.POST.get('descripcion'), direccion= request.POST.GET('descripcion'), precio= request.POST.get('precio'), ubicacion= Ubicacion.objects.get(barrio= request.POST.get('barrio')))  
    propiedad_intance.save()
    for filename in request.FILES.iteritems():
        name = request.FILES[filename].name
        print('name =' + name)  ## <-- not printing anything
        print('file = ' +  file)## <-- not printing anything
        print('filename = ' + filename)## <-- not printing anything
  ubicaciones = Ubicacion.objects.all()
  ctx = {'ubicaciones': ubicaciones}
  return render(request, 'main/add_modal.html', ctx)

HTML 模板

<form method='post' action=''  enctype="multipart/form-data">
   <input name='imagen' type="file" multiple/>
   <button type='submit' class="btn waves-effect">Upload</button>
</form>

views.py 是我现在拥有的,但到目前为止我已经尝试了以下所有变体:

**1**
if request.method == 'POST':
  for f in request.FILES.getlist('imagen'):
    filename = f.name
    print(filename) ## <-- not printing anything

**2**
if request.method== 'POST':
  form = FileUploadForm(request.POST, request.FILES) ## form imported from forms.py
  if form.is_valid():
     print('form is valid!') ## <-- not printing anything
  else:
     print('form not valid ') ## <-- not printing anything either!! IDK WHY
**3**
if request.method == 'POST':
  print('request.method = "POST" checked ') # <-- Not priting anything! but the model below is being saved to the database! my brain is about to explode right now haha.
  propiedad_instance = Propiedad(tipo = request.POST.get('tipo'), tamano=request.POST.get('tamano'), habitaciones = request.POST.get('habitaciones'), banos = request.POST.get('banos'), descripcion= request.POST.get('descripcion'), direccion= request.POST.GET('descripcion'), precio= request.POST.get('precio'), ubicacion= Ubicacion.objects.get(barrio= request.POST.get('barrio')))    
  propiedad_intance.save()
  files = request.FILES.getlist('imagen')
    if files:
        for f in files:
            print('something') #<-- not printing anything
            print(f) #<-- not printing anything
            print(f.name) <-- not printing anything
    else:
        print('nothing here') #<--- not printing anything   

提交表单后的控制台日志 enter image description here

【问题讨论】:

  • 欢迎来到 Stack Overflow。请修复您的缩进,因为目前尚不清楚它是复制/粘贴问题还是您的缩进本身就是问题。
  • 很抱歉。
  • 添加了整个views.py,add_prop所做的是在一个模式中显示一个表单,让你在一个真实的网站上添加一个新的房子/公寓,所有的输入字段都被正确保存在Propiedad 模型,但我注意到文件没有上传到我的媒体根目录,当时我更改了代码以查看 request.FILES 在提交表单时的值,但似乎 request.FILES 为空或类似的东西,因为每次我尝试在 if 或 for 循环调用 request.FILES 中打印一些东西时,都不做任何事情,控制台上没有错误..
  • 即使尝试使用 if 语句,例如: if request.FILES: ... else : ... else 语句也不会返回任何内容,就像每次我调用 request.FILES django 都会忽略指向它的代码行。

标签: python django http upload request


【解决方案1】:

根据文档上传多个文件的正确方法是..

files = request.FILES.getlist('file_field') #'file_field' --> 'imagen' for you
if form.is_valid():
        for f in files:
            ...  # Do something with each file.

您的 html 看起来不错。按照上述替换您的视图代码并检查其是否在视图中生成 POST?

Django Docs - Multiple File Upload

【讨论】:

  • 试过了,但也没有用...我在问题的最后部分添加了我尝试过的内容 3 ,如果我犯了任何错误,请告诉我
  • 你的 3 号看起来不错。当您检查 request.POST 时,您可以立即添加日志/打印语句吗?您还可以将整个 view 包括 post 请求添加到代码中吗?
  • 当然!更新了视图 3 并在提交表单后上传了控制台的图片,if 语句后的 print() 没有运行,但正在保存下面一行中的模型!我觉得我现在快疯了哈哈
  • 你在使用 jquery/javscript 吗?可能是使用 JQuery .post() 捕获和处理提交,因此数据实际上并未作为 multipart/form-data 发送。
  • 这可能是原因!我正在使用 jquery .load('url to my view') 来填充在主页中弹出的模式,所以我认为你是对的!感谢一百万帮助我!
猜你喜欢
  • 2011-11-09
  • 2013-02-09
  • 2015-04-12
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2011-01-01
相关资源
最近更新 更多