Django Tastypie - serving emoji that are stored in db

我的梦境 提交于 2019-12-12 01:35:26

问题


I'm using django and tastypie for my api. the db is MySQL.

After a few days of fiddling around i managed to store emoji icons in the db using utf8mb4 character set.

When querying the db directly from the console (on a mac), i see the emoji fine, but when pulling them from the api (for example using the browser), the json shows question marks. This leads me to believe the issue is not with the db but with django/tastypie db connection.

How do i go about it?


回答1:


The solution is in DJango settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {'charset': 'utf8mb4'},
        (...)



回答2:


What if you save the JSON dump to a text file and open it with a UTF8 text editor? Will the emoji re-appear?

I suspect that, when displaying the JSON in the browser, the browser falls back to Latin-1 encoding, thus ending up with question marks. There might be a setting in your browser to set the encoding.

Check the "Content-Type" header that comes from your server. Does it say application/json; encoding=utf-8?



来源:https://stackoverflow.com/questions/15178533/django-tastypie-serving-emoji-that-are-stored-in-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!