在过去的几天里,我一直在为此绞尽脑汁,并且无法为我的具体问题找到太多帮助。这是我的设置。
Windows 7
IIS 7.5
Helicon Zoo 3.1.98.508
Django 1.6.3
Python 2.7.3
我应该说我是 Django 和 Helicon Zoo 的新手。
发生的事情是,虽然我在默认的 2.5mb 下上传文件似乎没有问题,但对于超过这个数量的任何内容,我都无法正确上传块。虽然我确信我可以在我的设置文件中提高这个限制,但我真的不想因为我的目的而需要上传比我真正希望由内存处理的文件更大的文件。
当我尝试这样做时,我看到的是来自 Helicon Zoo 的一个相当无信息的错误 500,关于一些丢失的标题
Worker Status
The process was created
Windows error
The operation completed successfully. (ERROR CODE: 0)
Internal module error
message: HTTP-headers - are expected
type: ZooException
file: Jobs\JobFastCgi.cpp
line: 391
version: 3.1.98.508
STDERR
Empty stderr
起初,我认为这可能与临时文件夹有关,所以我设置了
FILE_UPLOAD_TEMP_DIR = [os.path.join(BASE_DIR, 'temp')]
在我的设置文件中,但它似乎没有做任何有用的事情。
然后,我发现了如何为 django 启用日志记录,这真的让我大开眼界。我现在知道错误来自哪里..但我不知道我应该怎么做才能解决它..
[29/Apr/2014 11:37:00] ERROR [django.request:226] Internal Server Error: /upload/
Traceback (most recent call last):
File "E:\mysite\venv\lib\site-packages\django\core\handlers\base.py", line 107, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "E:\mysite\venv\lib\site-packages\django\middleware\csrf.py", line 170, in process_view
request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
File "E:\mysite\venv\lib\site-packages\django\core\handlers\wsgi.py", line 146, in _get_post
self._load_post_and_files()
File "E:\mysite\venv\lib\site-packages\django\http\request.py", line 215, in _load_post_and_files
self._post, self._files = self.parse_file_upload(self.META, data)
File "E:\mysite\venv\lib\site-packages\django\http\request.py", line 180, in parse_file_upload
return parser.parse()
File "E:\mysite\venv\lib\site-packages\django\http\multipartparser.py", line 197, in parse
charset)
File "E:\mysite\venv\lib\site-packages\django\core\files\uploadhandler.py", line 135, in new_file
self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0, self.charset)
File "E:\mysite\venv\lib\site-packages\django\core\files\uploadedfile.py", line 61, in __init__
dir=settings.FILE_UPLOAD_TEMP_DIR)
File "E:\mysite\venv\lib\site-packages\django\core\files\temp.py", line 27, in __init__
dir=dir)
File "C:\python27\lib\tempfile.py", line 300, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\python27\lib\tempfile.py", line 235, in _mkstemp_inner
fd = _os.open(file, flags, 0600)
TypeError: coercing to Unicode: need string or buffer, list found
这是什么意思?我的块是否以某种方式作为列表对象进入而我没有意识到?
这是我的应用程序代码,它包含在我制作的博客应用程序中,因为它是我的主要应用程序,它似乎比创建一个自己的应用程序更简单。那是一个错误吗?
表格.py
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
视图.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import RequestContext, loader
from blog.forms import UploadFileForm
from blog.models import Blog
from blog.uploads import handle_uploaded_file
def index(request):
latest_blog_list = Blog.objects.order_by('-pub_date')[:5]
template = loader.get_template('blog/index.html')
context = RequestContext(request, {
'latest_blog_list': latest_blog_list,
})
return HttpResponse(template.render(context))
def detail(request, blog_id):
try:
blog = Blog.objects.get(pk=blog_id)
except:
raise Http404
return render(request, 'blog/detail.html', {'blog': blog})
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
#form.save()
return HttpResponseRedirect('/upload/')
else:
form = UploadFileForm()
return render_to_response('upload/upload.html', {'form': form}, context_instance=RequestContext(request))
上传.py
import os
saveDir = 'E:\\uploads\\'
def handle_uploaded_file(f):
#logging.debug('upload_here')
if f:
destination = open(saveDir + f.name, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
应用程序 urls.py
from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<blog_id>\d+)/$', views.detail, name='detail'),
url(r'^upload/$', views.upload_file, name='upload'),
)
项目网址.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^/?', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
url(r'^upload/', include('blog.urls', namespace="upload")),
)
我还应该注意,这些大文件的一部分实际上出现在我的上传位置,但大小不超过 1 兆。就像由于某种原因,该过程在实际完成之前就停止了。然后我必须停止并重新启动 IIS 才能使站点再次运行。