2016-04-15 57 views
1

我最近將某個詞(字符串)切換爲非英語語言。現在我在控制檯中出現這個錯誤。我正在使用python,django。這發生在ajax上。我相信在Python文件中,我只需要在頂部添加「# - - 編碼:utf-8-- 」。我對ajax的位置做了同樣的事情,但它什麼也沒做。 這裏; S完整的錯誤UnicodeEncodeError在/ notifications/ajax /'ascii'編解碼器無法編碼字符:序號不在範圍內(128)

UnicodeEncodeError at /notifications/ajax/ 
'ascii' codec can't encode character u'\ubbbb' in position 98: ordinal not in range(128) 
Traceback: 
File "/home/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/home/env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    22.     return view_func(request, *args, **kwargs) 
File "/home/notifications/views.py" in get_notifications_ajax 
    47.   notes.append(str(note.get_link)) 

Exception Type: UnicodeEncodeError at /notifications/ajax/ 
Exception Value: 'ascii' codec can't encode character u'\ubbbb' in position 98: ordinal not in range(128) 

這裏; S爲Ajax

@login_required 
def get_notifications_ajax(request): 
    if request.is_ajax() and request.method == "POST": 
     notifications = Notification.objects.all_for_user(MyProfile.objects.get(user=request.user)).recent() 
     count = notifications.count() 
     notes = [] 
     for note in notifications: 
      notes.append(str(note.get_link)) 
     data = { 
      "notifications": notes, 
      "count": count, 
     } 
     print data 
     json_data = json.dumps(data) 
     print json_data 
     return HttpResponse(json_data, content_type='application/json') 
    else: 
     raise Http404 

錯誤就是從這裏存在的功能;

notes.append(str(note.get_link)) 

這裏是我的ajax

<script> 
    $(document).ready(function(){ 
     $(".notification-toggle").click(function(e){ 
     e.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      url: "{% url 'get_notifications_ajax' %}", 
      data: { 
      csrfmiddlewaretoken: "{{ csrf_token }}", 
      }, 
     success: function(data){ 
      $("#notification_dropdown").html(' <li role="presentation" class="dropdown-header">view</li>'); 
      var count = data.count 
      console.log(count) 
      if (count == 0) { 
       var url = '{% url "notifications_all" %}' 
       $("#notification_dropdown").append("<li><a href='" + url+ "'>view all</a></li>") 
      } else { 
       $(data.notifications).each(function(){ 
       var link = this; 
       $("#notification_dropdown").append("<li>" + link + "</li>") 
       }) 
      } 
      console.log(data.notifications); 
      }, 
      error: function(rs, e) { 
      console.log(rs); 
      console.log(e); 
      } 
     }) 
     }) 
    }) 
    </script> 

我換什麼是 「動詞」 就這一個: notify.send(MyProfile.objects.get(用戶= request.user), 行動= new_comment , 目標= parent_comment, 收件人= parent_comment.user, affected_users = affected_users, 動詞= '回答')

我翻譯'已回覆'爲韓文,並且我得到了錯誤

回答

2

對韓文字符使用str將在Python 2中失敗 - 您應該使用.encode()。因此,將str(note.get_link)更改爲note.get_link.encode('utf-8')應該可以正常工作。

Check out the unicode how to in the docs

>>> x = u'\ubbbb' 
>>> x 
u'\ubbbb' 
>>> type(x) 
<type 'unicode'> 
>>> str(x) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
UnicodeEncodeError: 'ascii' codec can't encode character u'\ubbbb' in position 0: ordinal not in range(128) 
>>> x.encode('utf-8') 
'\xeb\xae\xbb' 
+0

啊,哈謝謝,我會嘗試這種 –

+0

應該像STR(note.get_link.encode( 'UTF-8'))吧? –

+0

謝謝你現在工作的很好 –

相關問題