Django图片上传表格不起作用

时间:2017-01-12 12:26:56

标签: html django python-2.7 django-forms image-uploading

我无法在Django Form中上传图片。 由于图片上传中的某些问题,以下表单无法验证

以下是代码:

forms.py

from django import forms

from .models import Description, Bill


class DForm(forms.ModelForm):
 class Meta:

    model = Description
    fields = ('desc', 'billing', 'length', 'quality', 'rate')

class BForm(forms.ModelForm):
 party = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Party'}))
 inovice = forms.FloatField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Invoice#'}))
 amount = forms.FloatField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Bill-Amount'}))
 image = forms.ImageField()
 image_caption = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Description for bill'}))
 class Meta:
    model = Bill
    fields = ('party', 'inovice', 'amount','image','image_caption')

models.py

from __future__ import unicode_literals
#from django.utils.encoding import python_2_unicode_compatible


from django.db import models
#@python_2_unicode_compatible
class Description(models.Model):
 desc = models.CharField(max_length = 200,default = "Suits")
 billing = models.FloatField(max_length = 200)
 length = models.FloatField(max_length = 200)
 quality = models.CharField( max_length=200,default = "custom")
 rate = models.FloatField(max_length = 200)

 def __str__(self):
    return self.desc

class Bill(models.Model):
 party = models.CharField(max_length = 200)
 inovice = models.FloatField(max_length = 200)
 amount = models.FloatField(max_length = 200)
 image = models.ImageField(upload_to='images/products/main',null=True, blank=True)
 image_caption = models.CharField(max_length=200,default = "null")

 def __str__(self):
    return self.party+','+str(self.inovice)+','+str(self.amount)
 def bill_id(self):
    return self.id;

index.html

{% load staticfiles %}
<!-- test -->
<html>
<head>

    <title></title>
    <link href="{%static "./styles/bootstrap.min.css" %}" rel="stylesheet" />
</head>
<body>
    <h1 style="text-align: center;">Rahul's Shop</h1>
    <h2 style="text-align: center;">Inventory</h2>
    <form  enctype="multipart/form-data" id="bill" action ="{% url 'front:commitbill' %}" method = "post" class="form-horizontal">

        {% csrf_token %}

        {% for field in form %}
            <div class="form-group">
            <label for="party" class="control-label col-md-3">{{ field.name }}</label>
            <div class="col-md-4">
                {{ field }}
            </div>
            <div class="col-md-5">
                <span id="one" style="position:relative; top:5px; color:red "></span>
            </div>
        </div>
        {% endfor %}

        <div class="container">
            <div class="row">
                <input type="button" id="add_items" class="col-md-offset-5 col-md-2  btn btn-success" value="Add items" \>
            </div>
        </div>

    </form>

和views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Description, Bill
from django.http import Http404
from .forms import DForm
from .forms import BForm
import pprint

def bill(request):
 try:
    billinfo = Description.objects.all()
    context = {'billinfo': billinfo}
 except Description.DoesNotExist:
    raise Http404("No Bills")
 return render(request, 'front/bill.html', context)

def commitvalues(request):
 if request.method == "POST":
    form = DForm(request.POST)
    if form.is_valid():
        Description = form.save()
        print Description
        return HttpResponse("Hello;You're at the front index.Commit's done!")
    print form.errors
    return HttpResponse("Fail")
def commitbill(request):
 form = BForm()
 if request.method == "POST":
    form = BForm(request.POST,request.FILES)
    if form.is_valid():
        Bill = form.save()
        return HttpResponse(str(Bill.bill_id()))
    else:
         print form.errors
         form = BForm()
 return render(request, 'front/index.html', {
    'form': form
 })

print form.errrs返回以下两个错误:

<ul class="errorlist"><li>image<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

当我从forms.py中删除image = forms.ImageField()时表单开始正常工作,只是图像没有上传,否则当我包含image = forms.ImageField()时,表单永远不会由于上述错误

而保存

0 个答案:

没有答案