我最近將某個詞(字符串)切換爲非英語語言。現在我在控制檯中出現這個錯誤。我正在使用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, 動詞= '回答')
我翻譯'已回覆'爲韓文,並且我得到了錯誤
啊,哈謝謝,我會嘗試這種 –
應該像STR(note.get_link.encode( 'UTF-8'))吧? –
謝謝你現在工作的很好 –