2016-01-29 7 views
6

के बाद, निम्न ठीक काम करता है:UnicodeDecodeError Django-आराम-ढांचा 2 के तहत django-आराम-ढांचे के उन्नयन के 3

from rest_framework import rest_response, generics 
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer 

class SomeView(generics.GenericAPIView): 
    renderer_classes = JSONRenderer, BrowsableAPIRenderer 

    def get(self, request, *args, **kwargs): 
     ... 
     # Build a response dict with non-ascii in it 
     ... 
     return rest_response.Response(some_dict_with_non_ascii_in_it_somewhere) 

मैं स्पष्ट रूप से किसी भी गैर-ascii एन्कोड करने के लिए नहीं था ...

हालांकि, डीआरएफ 3 के उन्नयन के बाद, एक ही कोड अब निम्न त्रुटि फेंकता है:

Traceback (most recent call last): 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__ 
    return self.application(environ, start_response) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__ 
    return self.application(environ, start_response) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__ 
    response = self.get_response(request) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response 
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception 
    return debug.technical_500_response(request, *exc_info) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response 
    six.reraise(exc_type, exc_value, tb) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response 
    response = response.render() 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/template/response.py", line 158, in render 
    self.content = self.rendered_content 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/response.py", line 71, in rendered_content 
    ret = renderer.render(self.data, media_type, context) 
    File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/renderers.py", line 104, in render 
    separators=separators 
    File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps 
    sort_keys=sort_keys, **kw).encode(obj) 
    File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 210, in encode 
    return ''.join(chunks) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 671: ordinal not in range(128) 

मैं डीआरएफ 3 अनुमान लगा रहा हूँ कुछ नए config मूल्य, कहीं, कि डिफ़ॉल्ट डीआरएफ 2. मैं तहत किया गया था अब है मैंने बसने की कोशिश की जी REST_FRAMEWORK UNICODE_JSONTrue पर सेटिंग, लेकिन मुझे अभी भी एक ही त्रुटि मिलती है ...

क्या ऐसी कोई सेटिंग है जो उस टुकड़े को डीआरएफ 2 के रूप में व्यवहार कर सकती है? या क्या डीआरएफ 3 को मुझे अपने शब्दकोश में गैर-असीसी चरित्र का शिकार करने और मैन्युअल रूप से एन्कोड करने की आवश्यकता है?

उत्तर

9

मुझे जवाब मिला।

डीआरएफ 2, rest_framework.JSONRenderer.ensure_ascii में True पर सेट है। डीआरएफ 3 में, rest_framework.JSONRenderer.ensure_asciinot api_settings.UNICODE_JSON पर सेट किया गया है (जब मैंने प्रश्न लिखा था, तो मैंने पहले not को याद किया था ...)।

तो यह डीआरएफ 2 की तरह व्यवहार करने के लिए प्राप्त करने के लिए, मैं False बजाय True करने के लिए 'UNICODE_JSON' स्थापित करने के लिए की जरूरत है, जैसे मैं पहले की कोशिश की थी (यह सच है डिफ़ॉल्ट रूप से है):

REST_FRAMEWORK = { 
    ... 
    'UNICODE_JSON': False 
} 

वैकल्पिक रूप से, मैं, निश्चित रूप से, मेरे शब्दकोश मूल्यों को एन्कोड कर सकता था, जो कि कई मामलों में बेहतर विकल्प हो सकता है।

2

डिफ़ॉल्ट रूप से पायथन 2.7 मानते हैं कि तार बाइनरी हैं। आपकी फ़ाइलों के शीर्ष पर जोड़ने का प्रयास करें:

from __future__ import unicode_literals 

कि डिफ़ॉल्ट रूप से अपने तार यूनिकोड कर देगा और उन्हें सही ढंग से परिवर्तित मिल मदद करनी चाहिए।

+0

धन्यवाद। मैंने इसे आज़मा दिया, और जब उसने इसे बनाया, तो सभी प्रासंगिक स्ट्रिंग अब निर्देश के भीतर यूनिकोड हैं, लाइब्रेरी कोड जो प्रतिक्रिया ऑब्जेक्ट को संसाधित करता है और स्ट्रिंग में निर्देश को बदलता है, उसमें आयात कथन नहीं होता है, इसलिए सामान '' 'join() 'से पहले यूनिकोड और नियमित तारों के मिश्रण में वापस परिवर्तित हो जाता है ... – Troy

संबंधित मुद्दे