我没有任何错误。
好的,谁看错了?因为我不知道这是我的代码:

Views.py:

class UpdateFeaturete(generic.UpdateView):
    template_name = 'images/updatefeaturete.html'
    model = FeatureteImage
    fields = ['header', 'body', 'image']

    def get_object(self):
        return get_object_or_404(FeatureteImage, pk=self.kwargs['f_pk'])

    def form_valid(self, form):
        return super(UpdateFeaturete, self).form_valid(form)


models.py:

class FeatureteImage(models.Model):
    park = models.ForeignKey(Park)
    header = models.CharField(max_length=50, blank=True)
    body = models.TextField(max_length=250, blank=True)
    image = models.ImageField(upload_to='featureteimages')

def save(self, *args, **kwargs):
    park = Park.objects.get(id=self.venue.id)
    if park.featurete >= 3:
        pass
    else:
        park.featurete += 1
        park.save()
        super(FeatureteImage, self).save(*args, **kwargs)


def get_absolute_url(self):
    return reverse('park-features', kwargs={'pk': self.park.pk,})


我的模板:

<form method="post" action="" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="form-group">
            {{ form|crispy }}
        </div>
        <input type="submit" value="Save Changes." class="btn btn-primary" />
</form>


一切似乎都正常工作。除ImageField外,所有其他字段似乎都在更新。
先感谢您!

最佳答案

好的,所以您所需要的不是保存方法来覆盖clean方法:

def clean(self):
    park = Park.objects.get(id=self.venue.id)
    if park.featurete >= 3:
        raise ValidationError('error message goes here')

关于python - Django UpdateView不上传ImageField,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34186395/

10-09 22:28