KeyError 'image' 表单集

KeyError 'image' formsets

这是我第一次使用表单集/图像,这是我的错误:

 KeyError at /houses/new/

 'image'

这是我的代码:

models.py

class House(models.Model):
    user = models.ForeignKey(User, related_name='houses', on_delete=models.CASCADE)
    address = models.CharField(max_length=500)
    type = models.CharField(default='House', max_length=100)
    stories = models.IntegerField()
    square_feet = models.IntegerField()
    description = models.TextField()


# Class is for the houses images
class Image(models.Model):
    house = models.ForeignKey(House, default=None, related_name="images", on_delete=models.CASCADE)
    image = models.ImageField(verbose_name='image')

forms.py

# This is the blueprint for House forms
class AlbumForm(forms.ModelForm):
    address = forms.CharField(label="Address:")
    type = forms.CharField(label="Type of House (House, Condo, Cottage):")
    stories = forms.IntegerField(label="House Stories:")
    square_feet = forms.IntegerField(label='Square Feet:')
    class Meta:
        model = House
        fields = ['address', 'type', 'stories', 'square_feet', 'description']


# This is the form for images
class ImageForm(forms.ModelForm):
    image = forms.ImageField(label='Image')

    class Meta:
        model = Image
        fields = ('image',)

views.py

def post_house(request):
    ImageFormSet = modelformset_factory(Image, form=ImageForm, extra=10)

    if request.method == 'POST':
        house_form = AlbumForm(request.POST)
        formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none())

        if house_form.is_valid() and formset.is_valid():
            post_form = house_form.save(commit=False)
            post_form.user = request.user
            post_form.save()

            for form in formset.cleaned_data:
                image = form['image']
                photo = Image(house=post_form, image=image)
                photo.save()

            messages.success(request, "New house listing success")
            house = post_form
            return redirect('houses:details', house_id=house.pk)
        else:
            return render(request, 'login.html')
    else:
        house_form = AlbumForm()
        formset = ImageFormSet(queryset=Image.objects.none())
    return render(request, 'houses/house_form.html', {'house_form': house_form, 'formset': formset})

house_form.html

{% extends 'base.html' %}

{% block content %}
<br>
<div class="container">
    <h4>Post a New Home</h4>
    <form id="post_form" method="post" action="" enctype="multipart/form-data">

        {% csrf_token %}
        {{house_form}}
        {{ formset.management_form }}
        {% for form in formset %}
            {{ form }} <br>
        {% endfor %}


        <input type="submit" name="submit" class="btn btn-success" value="Submit" />
    </form>
</div>

{% endblock %}

这一定是我的表单键和表单集中每个表单所采用的内容之间的某种关系。话虽如此,它们都是 'image',所以我看不出问题所在。如果您有想法,请告诉我。万分感谢!

试试下面的代码,

if formset.is_valid():
   for form in formset:
       data = form.cleaned_data
       image = data.get('image')
       photo = Image(house=post_form, image=image)
       photo.save()