django模型中的UnicodeDecodeError

时间:2014-06-06 09:56:52

标签: python django django-models

我正在尝试在django中构建一个测验应用,但我在使用portguese中的特殊字符的字符串数据时收到此错误。

我在我的django应用中收到此错误:

UnicodeDecodeError at /ortografia/take/
'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)

这是追溯

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/ortografia/take/?guess=1

Django Version: 1.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'simulado',
 'south',
 'multichoice',
 'django.contrib.admin',
 'social_auth',
 'payments')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/filipeferminiano/Documents/django/quiz/quiz/simulado/views.py" in quiz_take
  106.             return user_load_next_question(request, previous_sitting, quiz)
File "/Users/filipeferminiano/Documents/django/quiz/quiz/simulado/views.py" in user_load_next_question
  225.         previous = question_check_user(request, quiz, sitting)  
File "/Users/filipeferminiano/Documents/django/quiz/quiz/simulado/views.py" in question_check_user
  391.         user_progress_score_update(request, question.category, 1, 1)
File "/Users/filipeferminiano/Documents/django/quiz/quiz/simulado/views.py" in user_progress_score_update
  415.     progress.update_score(category, score, possible)
File "/Users/filipeferminiano/Documents/django/quiz/quiz/simulado/models.py" in update_score
  260.             temp = temp + str(category_queried) + "," + str(score_to_add) + "," + str(possible_to_add) + ","

Exception Type: UnicodeDecodeError at /ortografia/take/
Exception Value: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)

这是models.py中的update_score

 def update_score(self, category_queried, score_to_add, possible_to_add):                                               
        category_test = Category.objects.filter(category=category_queried).exists()

        if category_test == False:
            return "error",  "category does not exist"  #  to do: make useful

        my_regex = re.escape(str(category_queried)) + r",(\d+),(\d+),"  #  group 1 is score, group 2 is possible

        match = re.search(my_regex, self.score, re.IGNORECASE)

        if match:
            current_score = int(match.group(1))
            current_possible = int(match.group(2))

            updated_current_score = current_score + score_to_add  #  add on the score
            updated_current_possible = current_possible + possible_to_add  #  add the possible maximum score

            new_score = str(category_queried) + "," + str(updated_current_score) + "," + str(updated_current_possible) + ","

            temp = self.score
            found_instance = match.group()
            temp = temp.replace(found_instance, new_score)  #  swap the old score for the new one

            self.score = temp
            self.save()


        else:
            """
            if not present but a verified category, add with the points passed in
            """
            temp = self.score
            temp = temp + str(category_queried) + "," + str(score_to_add) + "," + str(possible_to_add) + ","
            self.score = temp
            self.save()

0 个答案:

没有答案