【发布时间】: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