当我在我的formset中输入信息时,图像会被删除,我无法前进到下一页。页面上没有错误。基本上,表单数据没有通过" is_valid"。我非常肯定我的view.py中有错误。
Models.py
class Testimonial(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
image = VersatileImageField('image_testimonial', upload_to=upload_location, validators=[file_size], null=True, blank=True)
message = models.CharField(max_length=300, default='', null=True, blank=True)
name = models.CharField(max_length=100, default='', null=True, blank=True)
city = models.CharField(max_length=100, default='', null=True, blank=True)
state = models.CharField(max_length=100, default='', null=True, blank=True)
def __str__(self):
return self.author.username
forms.py
class TestimonialForm (forms.ModelForm):
class Meta:
model = Testimonial
fields = ('image', 'message', 'name', 'city', 'state',)
views.py
@login_required(login_url="/accounts/login/")
def testimonials(request):
TestimonialFormSet = modelformset_factory(Testimonial, form=TestimonialForm, extra=3)
if request.method == 'POST':
formset = TestimonialFormSet(request.POST or None, request.FILES or None)
if formset.is_valid():
instances = formset.save(commit=False)
for instance in instances: #for instance in instances.cleaned_data:
instance.author = request.user
fix_orientation(instance.image)
instance.save()
return redirect('/accounts/profile/')
else:
args = {'formset': formset}
return render(request, 'accounts/page6.html', args)
else:
formset = TestimonialFormSet(queryset=Testimonial.objects.filter(author=request.user))
return render(request, 'accounts/page6.html', {'formset': formset})
答案 0 :(得分:0)
我必须这样做 a)在我的模板中包含{{formset.errors}}以呈现阻止提交的错误
b)在模板中包含{{form.id}},即使id不是我模型的一部分。这似乎是一个奇怪的形式怪癖,但唉,我现在可以提交表格了。
向丹尼尔罗斯曼致以帮助我找到答案!