Thursday, March 1, 2012

UnicodeEncodeError in file uploads

Hi there,
for quite some time I got a Django project up and running on an Apache
web server. I got absolutely no problems with e.g. German umlauts. I
followed all instructions on how to set up django, apache and mysql.
But unfortunately uploading of files containing non-ASCII characters
raises a UnicodeEncodeError. In my code there is no need for any
conversion to ASCII: I'm simply using a ImageFileField in my model and
want to save the file.

I have absolutely no idea where to look for the problem. Any
suggestions???
The code is available on launchpad:
http://bazaar.launchpad.net/~andi-hasenkopf/aintnowiki/trunk/revision/75
Here is the DEBUG output:

UnicodeEncodeError at /photobook/upload/hdd/

'ascii' codec can't encode character u'\xf6' in position 30: ordinal
not in range(128)

Request Method: POST
Request URL: https://www-elektronenmikroskopie.uni-regensburg.de/photobook/upload/hdd/
Django Version: 1.3.1
Exception Type: UnicodeEncodeError
Exception Value:

'ascii' codec can't encode character u'\xf6' in position 30: ordinal
not in range(128)

Exception Location: /usr/lib/python2.6/genericpath.py in exists, line
18
Python Executable: /usr/bin/python
Python Version: 2.6.5
Python Path:

['/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/pymodules/python2.6',
'/usr/lib/pymodules/python2.6/gtk-2.0',
'/usr/local/lib/python2.6/dist-packages',
'/var/aintnowiki/',
'/var/']

Server time: Thu, 1 Mar 2012 10:47:41 +0100
Unicode error hint

The string that could not be encoded/decoded was: mps/höle_in
Traceback Switch to copy-and-paste view

/usr/local/lib/python2.6/dist-packages/django/core/handlers/
base.py in get_response

response = callback(request,
*callback_args, **callback_kwargs)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py
in _wrapped_view

response = view_func(request, *args,
**kwargs)

...
▶ Local vars
/var/aintnowiki/wiki/decorators.py in _wrapped_view_func

return view_func(request, *args, **kwargs)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/contrib/auth/
decorators.py in _wrapped_view

return view_func(request, *args, **kwargs)

...
▶ Local vars
/var/aintnowiki/stampit/views.py in upload_hdd

photo.save()

...
▶ Local vars
/var/aintnowiki/stampit/models.py in save

super(Photo,self).save(*args,**kwargs)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/db/models/base.py in
save

self.save_base(using=using, force_insert=force_insert,
force_update=force_update)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/db/models/base.py in
save_base

for f in meta.local_fields if not
isinstance(f, AutoField)]

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/db/models/fields/
files.py in pre_save

file.save(file.name, file, save=False)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/db/models/fields/
files.py in save

self.name = self.storage.save(name, content)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/core/files/
storage.py in save

name = self.get_available_name(name)

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/core/files/
storage.py in get_available_name

while self.exists(name):

...
▶ Local vars
/usr/local/lib/python2.6/dist-packages/django/core/files/
storage.py in exists

return os.path.exists(self.path(name))

...
▶ Local vars
/usr/lib/python2.6/genericpath.py in exists

st = os.stat(path)

...
▶ Local vars

Request information
GET

No GET data
POST
Variable Value
Upload

u'Upload!'

csrfmiddlewaretoken

u'd652017af517ec5684f7702df836b489'

tags

u''

description

u''

title

u'test'

FILES
Variable Value
local_image

<InMemoryUploadedFile: höle_in_membrane.jpg (image/jpeg)>

COOKIES
Variable Value
csrftoken

'd652017af517ec5684f7702df836b489'

sessionid

'a52f7d9254d313ffcfafd1e37d622b8b'

META
Variable Value
mod_wsgi.reload_mechanism

'0'

mod_wsgi.listener_port

'443'

HTTP_REFERER

'https://www-elektronenmikroskopie.uni-regensburg.de/photobook/upload/
hdd/
'

mod_wsgi.listener_host

''

SERVER_SOFTWARE

'Apache/2.2.14 (Ubuntu)'

SCRIPT_NAME

u''

SERVER_SIGNATURE

'<address>Apache/2.2.14 (Ubuntu) Server at www-
elektronenmikroskopie.uni-regensburg.de Port 443</address>\n'

REQUEST_METHOD

'POST'

PATH_INFO

u'/photobook/upload/hdd/'

SERVER_PROTOCOL

'HTTP/1.1'

QUERY_STRING

''

PATH

'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/
X11R6/bin'

SSL_TLS_SNI

'www-elektronenmikroskopie.uni-regensburg.de'

CONTENT_LENGTH

'56064'

HTTP_USER_AGENT

'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.2) Gecko/20100101
Firefox/10.0.2'

HTTP_CONNECTION

'keep-alive'

HTTP_COOKIE

'csrftoken=d652017af517ec5684f7702df836b489;
sessionid=a52f7d9254d313ffcfafd1e37d622b8b'

SERVER_NAME

'www-elektronenmikroskopie.uni-regensburg.de'

REMOTE_ADDR

'132.199.99.54'

GEOIP_COUNTRY_NAME

'Germany'

wsgi.url_scheme

'https'

PATH_TRANSLATED

'/var/aintnowiki/apache/django.wsgi/photobook/upload/hdd/'

SERVER_PORT

'443'

wsgi.multiprocess

True

SERVER_ADDR

'132.199.99.155'

DOCUMENT_ROOT

'/var/www'

mod_wsgi.process_group

''

SCRIPT_FILENAME

'/var/aintnowiki/apache/django.wsgi'

SERVER_ADMIN

'webmaster@hasenkopf2000.net'

SCRIPT_URI

'https://www-elektronenmikroskopie.uni-regensburg.de/photobook/upload/
hdd/
'

wsgi.input

<mod_wsgi.Input object at 0x7fe9c4397b30>

HTTP_DNT

'1'

HTTP_HOST

'www-elektronenmikroskopie.uni-regensburg.de'

SCRIPT_URL

'/photobook/upload/hdd/'

HTTPS

'1'

wsgi.multithread

False

mod_wsgi.callable_object

'application'

REQUEST_URI

'/photobook/upload/hdd/'

HTTP_ACCEPT

'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

wsgi.version

(1, 0)

GATEWAY_INTERFACE

'CGI/1.1'

wsgi.run_once

False

wsgi.errors

<mod_wsgi.Log object at 0x7fe9c3e25990>

REMOTE_PORT

'51326'

HTTP_ACCEPT_LANGUAGE

'en-us,en;q=0.5'

mod_wsgi.version

(2, 8)

GEOIP_COUNTRY_CODE

'DE'

CONTENT_TYPE

'multipart/form-data;
boundary=---------------------------102201031114817323861166079304'

mod_wsgi.application_group

'pc101100593x.uni-regensburg.de|'

mod_wsgi.script_reloading

'1'

wsgi.file_wrapper

''

CSRF_COOKIE

'd652017af517ec5684f7702df836b489'

HTTP_ACCEPT_ENCODING

'gzip, deflate'

Settings
Using settings module aintnowiki.settings
Setting Value
GRAPPELLI_INDEX_DASHBOARD

'aintnowiki.dashboard.CustomIndexDashboard'

SERVER_EMAIL

'root@localhost'

USE_L10N

False

USE_THOUSAND_SEPARATOR

False

LANGUAGE_CODE

'en-us'

ROOT_URLCONF

'aintnowiki.urls'

MANAGERS

(('Andreas Hasenkopf', 'webmaster@hasenkopf2000.net'),)

DEFAULT_CHARSET

'utf-8'

STATIC_ROOT

'/var/aintnowiki/media/static/'

TEST_DATABASE_CHARSET

None

PRODUCTIVE_ENVIRON

True

MESSAGE_STORAGE

'django.contrib.messages.storage.user_messages.LegacyFallbackStorage'

DATABASE_HOST

''

IGNORABLE_404_STARTS

('/cgi-bin/', '/_vti_bin', '/_vti_inf')

SEND_BROKEN_LINK_EMAILS

False

URL_VALIDATOR_USER_AGENT

'Django/1.3.1 (http://www.djangoproject.com)'

STATICFILES_FINDERS

('django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder')

FILEBROWSER_DIRECTORY

'uploads/'

SESSION_COOKIE_DOMAIN

None

SESSION_COOKIE_NAME

'sessionid'

FILEBROWSER_IMAGE_GENERATOR_PORTRAIT

[('tiny_', 64), ('smaller_', 128)]

COMMENTS_MODERATORS_GROUP

None

TIME_INPUT_FORMATS

('%H:%M:%S', '%H:%M')

DATABASES

{'default': {'ENGINE': 'django.db.backends.mysql',
'HOST': '',
'NAME': 'temwiki',
'OPTIONS': {},
'PASSWORD': '********************',
'PORT': '',
'TEST_CHARSET': None,
'TEST_COLLATION': None,
'TEST_MIRROR': None,
'TEST_NAME': None,
'TIME_ZONE': 'Europe/Berlin',
'USER': 'temwiki'}}

HOST_ADDR

'http://www-elektronenmikroskopie.uni-regensburg.de/'

FILE_UPLOAD_PERMISSIONS

None

FILE_UPLOAD_HANDLERS

('django.core.files.uploadhandler.MemoryFileUploadHandler',
'django.core.files.uploadhandler.TemporaryFileUploadHandler')

DEFAULT_CONTENT_TYPE

'text/html'

APPEND_SLASH

True

FIRST_DAY_OF_WEEK

0

DATABASE_ROUTERS

[]

ANW_MAIN

'wiki'

YEAR_MONTH_FORMAT

'F Y'

STATICFILES_STORAGE

'django.contrib.staticfiles.storage.StaticFilesStorage'

CACHES

{'default': {'BACKEND':
'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': ''}}

STAMPIT_DEFAULT_PHOTOS_PER_PAGE

8

FILEBROWSER_URL_ADMIN

'/admin/filebrowser/'

SESSION_COOKIE_PATH

'/'

FILEBROWSER_IMAGE_CROP_GENERATOR

[('cropped_', 64, 64), ('croppedthumbnail_', 128, 128)]

USE_X_FORWARDED_HOST

False

WIKI_THEME

'tem'

IGNORABLE_404_ENDS

('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico',
'.php')

HTTPS_SUPPORT

True

MIDDLEWARE_CLASSES

('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')

USE_I18N

True

THOUSAND_SEPARATOR

','

SECRET_KEY

'********************'

FILEBROWSER_DEBUG

True

LANGUAGE_COOKIE_NAME

'django_language'

FILE_UPLOAD_TEMP_DIR

None

TRANSACTIONS_MANAGED

False

LOGGING_CONFIG

'django.utils.log.dictConfig'

TEMPLATE_LOADERS

('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader')

TEMPLATE_DEBUG

True

AUTHENTICATION_BACKENDS

('django.contrib.auth.backends.ModelBackend',)

TEST_DATABASE_COLLATION

None

FORCE_SCRIPT_NAME

None

CACHE_BACKEND

'locmem://'

SESSION_COOKIE_SECURE

False

TINYMCE_SPELLCHECKER

True

CSRF_COOKIE_DOMAIN

None

FILE_CHARSET

'utf-8'

DEBUG

True

SESSION_FILE_PATH

None

DEFAULT_FILE_STORAGE

'django.core.files.storage.FileSystemStorage'

INSTALLED_APPS

['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'grappelli.dashboard',
'grappelli',
'filebrowser',
'django.contrib.admin',
'tinymce',
'tagging',
'dajaxice',
'dajax',
'aintnowiki.wiki',
'aintnowiki.stampit',
'aintnowiki.captcha',
'aintnowiki.users',
'aintnowiki.tem_status']

LANGUAGES_BIDI

('he', 'ar', 'fa')

DATABASE_ENGINE

''

DATABASE_NAME

''

COMMENTS_FIRST_FEW

0

FILEBROWSER_PATH_TINYMCE

'/var/aintnowiki/media/js/tiny_mce/'

PREPEND_WWW

False

SESSION_COOKIE_HTTPONLY

False

DATABASE_PORT

''

STAMPIT_DEFAULT_FRAME

None

DEBUG_PROPAGATE_EXCEPTIONS

False

TINYMCE_FILEBROWSER

True

MONTH_DAY_FORMAT

'F j'

LOGIN_URL

'/users/login/'

SESSION_EXPIRE_AT_BROWSER_CLOSE

False

TINYMCE_JS_URL

'/media/js/tiny_mce/tiny_mce_src.js'

TIME_FORMAT

'P'

WIKI_PAGELOCK_TIMEOUT

1800

FILEBROWSER_URL_WWW

'http://www-elektronenmikroskopie.uni-regensburg.de/media/uploads'

DATE_INPUT_FORMATS

('%Y-%m-%d',
'%m/%d/%Y',
'%m/%d/%y',
'%b %d %Y',
'%b %d, %Y',
'%d %b %Y',
'%d %b, %Y',
'%B %d %Y',
'%B %d, %Y',
'%d %B %Y',
'%d %B, %Y')

CSRF_COOKIE_NAME

'csrftoken'

EMAIL_HOST_PASSWORD

'********************'

PASSWORD_RESET_TIMEOUT_DAYS

'********************'

CACHE_MIDDLEWARE_ALIAS

'default'

SESSION_SAVE_EVERY_REQUEST

False

ADMIN_MEDIA_PREFIX

'/media/static/grappelli/'

NUMBER_GROUPING

0

WIKI_CAT_PAGES_PER_VIEW

15

SESSION_ENGINE

'django.contrib.sessions.backends.db'

CSRF_FAILURE_VIEW

'aintnowiki.users.views.my403'

COMMENTS_SKETCHY_USERS_GROUP

None

LOGIN_REDIRECT_URL

'/accounts/profile/'

WIKI_DEFAULT_PUBLIC_WRITE

False

FILEBROWSER_IMAGE_GENERATOR_LANDSCAPE

[('tiny_', 64), ('smaller_', 128)]

LOGGING

{'disable_existing_loggers': False,
'handlers': {'mail_admins': {'class':
'django.utils.log.AdminEmailHandler',
'level': 'ERROR'}},
'loggers': {'django.request': {'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True}},
'version': 1}

CACHE_MIDDLEWARE_KEY_PREFIX

''

LOCALE_PATHS

()

TEMPLATE_STRING_IF_INVALID

''

DISALLOWED_USER_AGENTS

()

COMMENTS_ALLOW_PROFANITIES

False

LOGOUT_URL

'/accounts/logout/'

EMAIL_USE_TLS

False

TEMPLATE_DIRS

('/var/aintnowiki/templates',
'/var/aintnowiki/filebrowser/templates',
'/var/aintnowiki/tinymce/templates')

FIXTURE_DIRS

()

EMAIL_HOST

'localhost'

DATE_FORMAT

'N j, Y'

MEDIA_ROOT

'/var/aintnowiki/media/'

ADMINS

(('Andreas Hasenkopf', 'webmaster@hasenkopf2000.net'),)

FORMAT_MODULE_PATH

None

DEFAULT_FROM_EMAIL

'webmaster@localhost'

WIKI_DEFAULT_PUBLIC_READ

True

STATICFILES_DIRS

()

MEDIA_URL

'http://www-elektronenmikroskopie.uni-regensburg.de/media/'

DATETIME_FORMAT

'N j, Y, P'

EMAIL_SUBJECT_PREFIX

'[Django] '

SITE_ID

1

DAJAXICE_DEBUG

True

ALLOWED_INCLUDE_ROOTS

()

FILEBROWSER_URL_TINYMCE

'/media/js/tiny_mce/'

DECIMAL_SEPARATOR

'.'

SHORT_DATE_FORMAT

'm/d/Y'

DATABASE_USER

''

TEST_DATABASE_NAME

None

TEST_RUNNER

'django.test.simple.DjangoTestSuiteRunner'

TIME_ZONE

'Europe/Berlin'

FILE_UPLOAD_MAX_MEMORY_SIZE

2621440

EMAIL_BACKEND

'django.core.mail.backends.smtp.EmailBackend'

DEFAULT_TABLESPACE

''

TEMPLATE_CONTEXT_PROCESSORS

('django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages')

FILEBROWSER_PATH_SERVER

'/var/aintnowiki/media/uploads'

SESSION_COOKIE_AGE

1209600

SETTINGS_MODULE

'aintnowiki.settings'

USE_ETAGS

False

TINYMCE_COMPRESSOR

False

LANGUAGES

(('ar', 'Arabic'),
('az', 'Azerbaijani'),
('bg', 'Bulgarian'),
('bn', 'Bengali'),
('bs', 'Bosnian'),
('ca', 'Catalan'),
('cs', 'Czech'),
('cy', 'Welsh'),
('da', 'Danish'),
('de', 'German'),
('el', 'Greek'),
('en', 'English'),
('en-gb', 'British English'),
('es', 'Spanish'),
('es-ar', 'Argentinian Spanish'),
('es-mx', 'Mexican Spanish'),
('es-ni', 'Nicaraguan Spanish'),
('et', 'Estonian'),
('eu', 'Basque'),
('fa', 'Persian'),
('fi', 'Finnish'),
('fr', 'French'),
('fy-nl', 'Frisian'),
('ga', 'Irish'),
('gl', 'Galician'),
('he', 'Hebrew'),
('hi', 'Hindi'),
('hr', 'Croatian'),
('hu', 'Hungarian'),
('id', 'Indonesian'),
('is', 'Icelandic'),
('it', 'Italian'),
('ja', 'Japanese'),
('ka', 'Georgian'),
('km', 'Khmer'),
('kn', 'Kannada'),
('ko', 'Korean'),
('lt', 'Lithuanian'),
('lv', 'Latvian'),
('mk', 'Macedonian'),
('ml', 'Malayalam'),
('mn', 'Mongolian'),
('nl', 'Dutch'),
('no', 'Norwegian'),
('nb', 'Norwegian Bokmal'),
('nn', 'Norwegian Nynorsk'),
('pa', 'Punjabi'),
('pl', 'Polish'),
('pt', 'Portuguese'),
('pt-br', 'Brazilian Portuguese'),
('ro', 'Romanian'),
('ru', 'Russian'),
('sk', 'Slovak'),
('sl', 'Slovenian'),
('sq', 'Albanian'),
('sr', 'Serbian'),
('sr-latn', 'Serbian Latin'),
('sv', 'Swedish'),
('ta', 'Tamil'),
('te', 'Telugu'),
('th', 'Thai'),
('tr', 'Turkish'),
('uk', 'Ukrainian'),
('ur', 'Urdu'),
('vi', 'Vietnamese'),
('zh-cn', 'Simplified Chinese'),
('zh-tw', 'Traditional Chinese'))

DEFAULT_INDEX_TABLESPACE

''

INTERNAL_IPS

()

STATIC_URL

'/media/static/'

EMAIL_PORT

25

SHORT_DATETIME_FORMAT

'm/d/Y P'

WIKI_MAINPAGE

'welcome'

TINYMCE_DEFAULT_CONFIG

{'body_id': 'pages_body',
'content_css': 'http://www-elektronenmikroskopie.uni-regensburg.de/
media/themes/tem/css/theme.css
',
'height': '30em',
'plugins':
'autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',
'relative_urls': 'true',
'skin': 'o2k7',
'skin_variant': 'silver',
'theme': 'advanced',
'theme_advanced_buttons1':
'bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,attribs,styleprops',
'theme_advanced_buttons2':
'fontselect,fontsizeselect,|,tablecontrols,',
'theme_advanced_buttons3':
'cut,copy,paste,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,',
'theme_advanced_buttons4':
'visualchars,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,preview,fullscreen,code,spellchecker',
'theme_advanced_resizing': 'true',
'theme_advanced_statusbar_location': 'bottom',
'theme_advanced_toolbar_align': 'left',
'theme_advanced_toolbar_location': 'top',
'width': '100%'}

ABSOLUTE_URL_OVERRIDES

{}

DATABASE_OPTIONS

{}

CACHE_MIDDLEWARE_SECONDS

600

BANNED_IPS

()

DAJAXICE_NOTIFY_EXCEPTIONS

True

DATETIME_INPUT_FORMATS

('%Y-%m-%d %H:%M:%S',
'%Y-%m-%d %H:%M',
'%Y-%m-%d',
'%m/%d/%Y %H:%M:%S',
'%m/%d/%Y %H:%M',
'%m/%d/%Y',
'%m/%d/%y %H:%M:%S',
'%m/%d/%y %H:%M',
'%m/%d/%y')

DATABASE_PASSWORD

'********************'

ADMIN_FOR

()

PROFANITIES_LIST

'********************'

EMAIL_HOST_USER

''

COMMENTS_BANNED_USERS_GROUP

None

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment