2011-04-07 115 views
6

这里是欣赏代码:Django的文件上传

def index(request): 
    if request.method == 'POST': 
     a=request.POST 
#  logging.debug(a["title"]) 
#  logging.debug(a["file"]) 
     #form = UploadFileForm() 
     form = UploadFileForm(request.POST, request.FILES) 
     #handle_uploaded_file(request.FILES['file']) 
     if form.is_valid(): 
      handle_uploaded_file(request.FILES['file']) 
      return HttpResponseRedirect('/') 
    else: 
     form = UploadFileForm() 
    return render('upload.html', {'form': form}) 


def handle_uploaded_file(file): 
# logging.debug("upload_here") 
    if file: 
     destination = open('/tmp/'+file.name, 'wb+') 
     #destination = open('/tmp', 'wb+') 
     for chunk in file.chunks(): 
      destination.write(chunk) 
     destination.close() 

这里是机型代码:

class UploadFileForm(forms.Form): 
    title = forms.CharField(max_length=50) 
    file = forms.FileField(type="file") 

这里是upload.html代码:

{% block upload %} 

<form enctype="multipart/form-data" method="post" action="/upload/"> 

    {% csrf_token %} 
    <table> 
     <tr><td> 
      <input type="file" value="title" name="title" id="title" /><br /> 
      <input type="submit" value="Submit" id="Save"/> 
     </td></tr> 
    </table> 
</form> 
{% endblock %} 

选择文件后,按提交按钮,出现错误:

AttributeError的在/上传/

'WSGIRequest' 对象有没有属性 '块'

请求方法:POST 请求URL:http://www.mywebsite.com/upload/ Django的版本:1.3 异常类型:AttributeError的 异常值:

'WSGIRequest'对象没有属性'块'

异常位置:/usr/src/wpcms/views.py在handle_uploaded_file中,第63行

任何想法我在这里做错了吗?我忘了设置线吗?或者,一条进口线? 谢谢。

settings.py是:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'cms.middleware.page.CurrentPageMiddleware', 
    'cms.middleware.user.CurrentUserMiddleware', 
    'cms.middleware.toolbar.ToolbarMiddleware', 
    'cms.middleware.media.PlaceholderMediaMiddleware', 
    'django.middleware.doc.XViewMiddleware', 
    'django_authopenid.middleware.OpenIDMiddleware', 
) 

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth', 
    'django.core.context_processors.debug', 
    'django.core.context_processors.i18n', 
    'django.core.context_processors.request', 
    'django.core.context_processors.media', 
    'cms.context_processors.media', 
    'django_authopenid.context_processors.authopenid', 
) 

CMS_TEMPLATES = (
# ('basic.html', 'Basic Template'), 
# ('template_1.html', 'Template One'), 
# ('template_2.html', 'Template Two'), 
    ('home.html', gettext('Default')), 
    ('about.html', gettext('About')), 
#  ('blog.html', gettext('blog')), 
    ('contact.html', gettext('Contact')), 
) 

ROOT_URLCONF = 'urls' 

CMS_APPLICATIONS_URLS = (
    ('cmsplugin_news.urls', 'News'), 
) 

CMS_NAVIGATION_EXTENDERS = (
    ('cmsplugin_news.navigation.get_nodes', 'News navigation'), 
) 

THUMBNAIL_PROCESSORS = (
    'easy_thumbnails.processors.colorspace', 
    'easy_thumbnails.processors.autocrop', 
    #'easy_thumbnails.processors.scale_and_crop', 
    'filer.thumbnail_processors.scale_and_crop_with_subject_location', 
    'easy_thumbnails.processors.filters', 
) 

CMS_MODERATOR = False 

TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, 'templates'), 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.admin', 
    'django.contrib.comments', 
    'registration', 
    'django_authopenid', 
    'cms', 
    'menus', 
    'mptt', 
    'appmedia', 
    'south', 
    'cms.plugins.text', 
    'cms.plugins.picture', 
    'cms.plugins.link', 
    'cms.plugins.file', 
    'easy_thumbnails', 
    'filer', 
    'cmsplugin_filer_file', 
    'cmsplugin_filer_folder', 
    'cmsplugin_filer_image', 
    'cmsplugin_filer_teaser', 
    'cmsplugin_filer_video', 
    'cms.plugins.snippet', 
    'cms.plugins.googlemap', 
    'publisher', 
    'reversion', 
    'cms.plugins.teaser', 
    'cms.plugins.video', 
    'cms.plugins.twitter', 
    'cmsplugin_facebook', 
    'cmsplugin_news', 
    'cmsplugin_comments', 
    'captcha', 
) 

回答

10

有多种问题。这是一个可行的固定版本:

1)更改模板的实际形式:

<form enctype="multipart/form-data" method="post" action="/upload/"> 
    {% csrf_token %} 
    <table> 
     {{form.as_table}} 
    </table> 
    <input type="submit" value="Submit" id="Save"/> 
</form> 

2)更新您的形式在FileField或删除不必要的类型:

class UploadFileForm(forms.Form): 
    title = forms.CharField(max_length=50) 
    file = forms.FileField() 

3)更新您的视图添加CSRF:

def index(request): 
    if request.method == 'POST': 
     a=request.POST 
     form = UploadFileForm(request.POST, request.FILES) 
     if form.is_valid(): 
      handle_uploaded_file(request.FILES['file']) 
      return HttpResponseRedirect('/') 
    else: 
     form = UploadFileForm() 

    c = {'form': form} 
    c.update(csrf(request)) 
    return render_to_response('upload.html', c) 

希望这有助于!

+0

非常感谢。视图中的csrf也是一个启示。当我应用所有这些更改时,只会显示一个提交按钮,并且不会显示文件选择字段。 – kirtcathey 2011-04-07 15:12:44

+0

认为它是我settings.py中的东西....仍然得到相同的错误,并且表单数据没有出现在HTML中。将设置添加到问题。 – kirtcathey 2011-04-07 15:21:15

+0

看起来你需要调试你的环境中发生了什么。这里有一些建议:1)将表单生成的HTML源与工作正常的HTML源进行比较,2)从WSGI切换到开发环境并尝试重现3)在视图中添加Python调试,方法是添加“import pdb; pdb .set_trace()“并检查请求中发生了什么 - 您可以在PDB会话中键入”request.FILES.keys()“以查看哪些文件是由浏览器发送的。 – mif 2011-04-07 17:25:54