Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango File Upload
    text
    copied!<p>Here is the code in views:</p> <pre><code>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() </code></pre> <p>Here is the code in models:</p> <pre><code>class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField(type="file") </code></pre> <p>Here is the code in upload.html:</p> <pre><code>{% block upload %} &lt;form enctype="multipart/form-data" method="post" action="/upload/"&gt; {% csrf_token %} &lt;table&gt; &lt;tr&gt;&lt;td&gt; &lt;input type="file" value="title" name="title" id="title" /&gt;&lt;br /&gt; &lt;input type="submit" value="Submit" id="Save"/&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; {% endblock %} </code></pre> <h1>After I select a file, then press the submit button, an error appears:</h1> <p>AttributeError at /upload/</p> <p>'WSGIRequest' object has no attribute 'chunks'</p> <p>Request Method: POST Request URL: <a href="http://www.mywebsite.com/upload/">http://www.mywebsite.com/upload/</a> Django Version: 1.3 Exception Type: AttributeError Exception Value: </p> <p>'WSGIRequest' object has no attribute 'chunks'</p> <h1>Exception Location: /usr/src/wpcms/views.py in handle_uploaded_file, line 63</h1> <p>Any ideas what I am doing wrong here? Am I forgetting a settings line? Or, an import line? Thank you.</p> <p>settings.py is:</p> <pre><code>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', ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload