简体   繁体   中英

ImageField Form doesn't work in django

I know that there's a lot of similar questions to mine on stackoverflow but none of them fixed my problem.

I have a form with an imagefield which doesn't work as it should redirect to the index page when it succeeds but it doesn't. I can create a payment with that image from the admin panel but the form doesn't work.

models.py

class Payment(models.Model):
    Address = models.CharField(max_length=255)
    Payment_ID = models.ImageField(upload_to='payproof')
    Status = models.CharField(max_length=5, default="X")
    Review_result = models.CharField(max_length=255, default="Not yet reviewed")
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created']

        def __unicode__(self):
            return u'%s'% self.Status

    def __str__(self):
        return self.Status

views.py

def new_payment(request):
    template ='payment.html'
    form = PayForm(request.POST or None)

    if form.is_valid():
        form.save()
        return redirect('index')

    else:
        form = PayForm()

    context = {
        'form' : form,
    }
    return render(request, template, context)

forms.py

class PayForm(forms.ModelForm):
    Payment_ID = forms.ImageField()

    class Meta:
        model = Payment
        fields = ['Address',
         'Payment_ID']

所以问题很简单,我正在执行request.POST但是img是一个文件,所以我要做的所有事情就是添加request.FILES

form = PayForm(request.POST, request.FILES or None) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM