文件不会上传并保存在数据库中

时间:2016-07-19 07:38:48

标签: python django

我在django中保存和上传文件时遇到了问题。我一直在阅读整个教程,以便开始运行。我一直坚持这个:

'tuple' does not support the buffer interface

问题似乎来自views.py,它在保存之间停止。我希望你能指出我正确的方向..

我的上传/ views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from upload.models import Files
from upload.forms import DocumentForm

# import sys, traceback

def index(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Files(file_path = request.FILES['file_path'])
            newdoc.save()
            # print(request)

        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse('upload.views.index'))
    else:
        form = DocumentForm() # A empty, unbound form

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

    # print(form)
    print(documents)
    # Render list page with the documents and the form
    return render_to_response(
        'upload/index.html', {'documents': documents, 'form': form}, RequestContext(request)
)

我的上传/ models.py

from django.db import models

# Create your models here.
class Files(models.Model):
    user_id = models.IntegerField()
    project_id = models.IntegerField()
    file_path = models.FileField(upload_to='documents/%Y/%m/%d')
    file_name = models.CharField(max_length=255)
    file_size = models.CharField(max_length=45)
    file_type = models.CharField(max_length=45)
    file_ext = models.CharField(max_length=10)
    file_width = models.SmallIntegerField()
    file_height = models.SmallIntegerField()
    file_tag = models.CharField(max_length=45)
    location = models.TextField()
    approved = models.IntegerField()
    archived = models.IntegerField()
    restored = models.IntegerField()
    backup = models.IntegerField()
    date_upload = models.DateTimeField()
    date_modified = models.DateTimeField()

    def __str__(self):
        return 'file_name:{0}'.format(self.file_name)

    class Meta:
        managed = False
        db_table = 'files'

上传/ forms.py

# -*- coding: utf-8 -*-
from django import forms

class DocumentForm(forms.Form):
    file_path = forms.FileField(
        label='Select a file',
        help_text='max. 42 megabytes'
    )

以下是完整的追溯:

    Environment:


Request Method: POST
Request URL: http://localhost:8000/upload/

Django Version: 1.9
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'gallery',
 'upload']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "G:\python\testify\upload\views.py" in index
  18.             newdoc.save()

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\base.py" in save
  697.                        force_update=force_update, update_fields=update_fields)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\base.py" in save_base
  725.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\base.py" in _save_table
  809.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\base.py" in _do_insert
  848.                                using=using, raw=raw)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\query.py" in _insert
  1037.         return query.get_compiler(using=using).execute_sql(return_id)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\sql\compiler.py" in execute_sql
  985.             for sql, params in self.as_sql():

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\sql\compiler.py" in as_sql
  943.                 for obj in self.query.objs

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\sql\compiler.py" in <listcomp>
  943.                 for obj in self.query.objs

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\sql\compiler.py" in <listcomp>
  941.                     ) for f in fields

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\fields\files.py" in pre_save
  311.             file.save(file.name, file, save=False)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\fields\files.py" in save
  90.         name = self.field.generate_filename(self.instance, name)

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\fields\files.py" in generate_filename
  332.         return os.path.join(self.get_directory_name(), self.get_filename(filename))

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\db\models\fields\files.py" in get_filename
  322.         return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\utils\functional.py" in inner
  205.             self._setup()

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\core\files\storage.py" in _setup
  333.         self._wrapped = get_storage_class()()

File "c:\python\lib\site-packages\django-1.9.dev20150917232253-py3.4.egg\django\core\files\storage.py" in __init__
  185.         self.location = abspathu(self.base_location)

File "c:\python\lib\ntpath.py" in abspath
  547.                 path = _getfullpathname(path)

Exception Type: TypeError at /upload/
Exception Value: 'tuple' does not support the buffer interface
  • 更新: 它现在保存在数据库中,我刚刚添加了newdoc中的所有字段。我的表不允许NULL值,但问题在于:

    file_path = request.FILES['file_path']
    

2 个答案:

答案 0 :(得分:3)

FileField表示上传的文件对象。所以你需要传递一个文件而不是路径

def index(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Files(file_path = form.cleaned_data['file_path'])
            newdoc.save()
            # print(request)

documentation on file uploads对此非常详细。

答案 1 :(得分:0)

我想我已经解决了我的问题,我发布了这个解决方案,以防有其他新手试图让这项工作成功。注意:您最好将文件字段的名称从'file_path'重命名为'file',该文件在我的DocumentForm中,以避免命名混淆

def index(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)

        if form.is_valid():
            # Upload the File
            uploaded_file(request)
            # Save in database
            newdoc = Files(
                file_path = 'media/'+request.FILES['file_path'].name,
                file_name = request.FILES['file_path'].name,
                file_size = request.FILES['file_path'].size,
                file_type = request.FILES['file_path'].content_type,
                # Other data fields here..
            )
            newdoc.save()

        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse('upload.views.index'))
    else:
        form = DocumentForm() # A empty, unbound form

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

    # Render list page with the documents and the form
    return render_to_response(
        'upload/index.html', {'documents': documents, 'form': form}, RequestContext(request)
    )

def uploaded_file(request):
    uploaded_filename = request.FILES['file_path'].name

    # save the uploaded file inside that folder.
    full_filename = 'media/'+uploaded_filename
    fout = open(full_filename, 'wb+')

    file_content = ContentFile( request.FILES['file_path'].read() )

    # Iterate through the chunks.
    for chunk in file_content.chunks():
        fout.write(chunk)
    fout.close()