如何解决MultiValueDictKeyError'icon'?

时间:2018-07-01 08:51:55

标签: python django

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Product
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.POST['icon'] and request.POST['image']:
            product = Product()
            product.title = request.POST['title']
            product.body = request.POST['body']
            product.url = request.POST['url']
            product.icon = request.FILES['icon']
            product.image = request.FILES['image']
            product.pub_date = timezone.datetime.now()
            product.hunter = request.user
            product.save()
            return redirect('home')
        else:
            return render(request, 'products/create.html', {'error':'all info is required'})
    else:
        return render(request, 'products/create.html')

1 个答案:

答案 0 :(得分:0)

大声笑,我解决了我的问题,因为

request.POST ['icon']和request.POST ['image']

该请求已发布,我尝试上传数据库中的文件

request.FILES ['icon']和request.FILES ['image']

这是正确的

感谢您的回复