如何解决错误'WSGIRequest'对象在django 2.0.2中没有属性'Files'

问题描述 投票:0回答:1

无法从Django中的请求中检索文件。在尝试上传图像和图标并尝试在视图中检索它时,获取'WSGIRequest'对象在django 2.0.2中没有属性'Files'。

from django.shortcuts import render,redirect
from django.contrib.auth.decorators import login_required
from .models import products
from django.utils import timezone

def home(request):
    return render(request,'products/home.html')

@login_required    
def create(request):
    if request.method == 'POST':
        if request.POST['title'] and request.POST['body'] and request.POST['url'] and request.Files['image'] and request.Files['icon']:
           product = products()
           product.title = request.POST['title']
           product.body =  request.POST['body']
           if request.POST['url'].startswith('http://') or request.POST['url'].startswith('https://'):
               product.url = request.POST['url']
           else:
               product.url = 'http://' + request.POST['url']

           product.image = request.Files['icon']
           product.icon = request.Files['image']
           product.date_pretty = timezone.datetime.now()
           product.hunter = request.User
           product.save()
           return redirect('home')


        else:
           return render(request,'products/create.html',{'error':'Please Fill all the Fields'})
    else:       
        return render(request,'products/create.html')    
python-3.x django-views
1个回答
0
投票

我想,您需要调用request.FILES而不是request.Files来访问这些文件。检查here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.