Search code examples
pythondjangoasciiencodeutf

Python/Django encoding issue


I have a problem which drives me crazy ...

I want to build a django calendar app. I created a site where I can upload ics files to feed the calendar with events. The focus should be the method to handle the upload. Here I save the uploaded file and want to parse it's content with iCalendar (python module). After I read the content I want to convert the events from the ics files into my events which I can save in the database. The error occurs when I want to save the module e.save() ... there are problems with german umlaute ö ä Ü ß etc... As you can see I already tried to encode the summary with utf-8 but still encounter the problem... and ideas?

thanks

My Form:

class UploadFileForm(forms.Form):
file = forms.FileField()

My Model

class Event(models.Model):
 title = models.CharField(max_length=255)
 start = models.DateTimeField()
 end = models.DateTimeField()
 url = models.CharField(max_length=255)

My View

if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        handle_upload(request)
        return HttpResponseRedirect(reverse("app:site"))
else:
    form = UploadFileForm()

return TemplateResponse(
    request,
    'app/site.html',
    {'form' : form},
)

My method to handle upload

fname = request.FILES['file'].name
f = request.FILES['file']

with open('app/media/import/%s' % fname, 'wb+') as destination:
    for chunk in f.chunks():
        destination.write(chunk)

g = open('app/media/import/%s' % fname, 'rb')

cal = Calendar.from_ical(g.read())

for event in cal.walk('vevent'):
    title = str(event.get('summary')).encode('utf-8')
    start = format_date(event.get('dtstart').to_ical())
    end = format_date(event.get('dtend').to_ical())

    e = Event(title=title, start=start, end=end)
    e.save() 

the Error

Environment:
Request Method: POST
Request URL: domain:8000/site/source/

Django Version: 1.6.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myApp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/mySite/myApp/views.py" in source
  24.             handle_upload(request)
File "/home/user/mySite/myApp/utils.py" in handle_upload
  20.         title = str(event.get('summary')).encode('utf-8')

Exception Type: UnicodeEncodeError at /calendar/source/
Exception Value: 'ascii' codec can't encode character u'\xdf' in position 3: ordinal not in range(128)

Solution

  • You should not use str() as it is where the ascii error comes from. Simply use encode. Or use unicode instead of str – Vajk Hermecz