Django上传表单问题,没有选择图像

时间:2014-07-09 14:32:57

标签: django image upload

我正试图在我的django 1.5应用程序中解决我的上传文件问题。我下载了一个例子,但我有一些问题。 当我按下ulpoad按钮时,它告诉我它是空的,但这不是真的......

这是我的上传表单:

class DocumentForm(forms.Form):
    docfile = forms.FileField(
    label='Select a file'

这是视图

def image(request, auction_event_id):
# Handle file upload
try:
    auction_event = AuctionEvent.objects.get(pk=auction_event_id)
except AuctionEvent.DoesNotExist:
    raise Http404
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        newdoc = Document(docfile = request.FILES['docfile'])
        newdoc.save()

        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse('com:auction_view_auction_event', args=(auction_event.pk,)))
else:
    form = DocumentForm() # A empty, unbound form

# Load documents for the list page
documents = Document.objects.all()

# Render list page with the documents and the form
return render_to_response(
    'auction/setimages.html',
    {'documents': documents, 'form': form},
    context_instance=RequestContext(request)
)

和模型

class Document(models.Model):
auction_event = models.ForeignKey(AuctionEvent, related_name='images')
docfile = models.FileField(upload_to='documents/%Y/%m/%d')

1 个答案:

答案 0 :(得分:1)

通常的原因是您忘记将enctype="multipart/form-data"添加到HTML表单标记中。请参阅the docs