2016-05-02 137 views
1

我目前使用Django-countries獲取表單字段的ISO國家/地區列表。它顯示沒有在我的網站的錯誤,但Django的管理站點它會導致上述錯誤由於非ASCII國名的存在:UnicodeEncodeError:'ascii'編碼解碼器無法在位置35編碼字符u' xc5':序號不在範圍內(128)

Unicode error hint: The string that could not be encoded/decoded was: itan Åland

我讀過類似的問題可以用encode('utf-8')屬性來解決,但是如果不需要聯繫Django Admin本身,這似乎不是很可行。如果有人能解決這個問題,我將不勝感激。

Models.py(消息):

 
    from __future__ import unicode_literals 
    from django.db import models 
    from auth.models import User 
    from tourlisting.models import Tour 

    class Itinerary(models.Model): 
     start_date = models.DateField() 
     end_date = models.DateField() 

     def __str__(self): 
      return "Itinerary " + str(self.id) 

    class TourActivity(models.Model): 
     start_time = models.DateTimeField() 
     end_time = models.DateTimeField() 
     description = models.CharField(max_length=500, default='') 
     location = models.CharField(max_length=200) 
     itinerary = models.ForeignKey(Itinerary) 

    class ThreadManager(models.Manager): 
     def create_thread(self, tour, itinerary, traveller): 
      thread = self.create(tour=tour, itinerary=itinerary, traveller=traveller) 
      return thread 

    class Thread(models.Model): 
     tour = models.ForeignKey(Tour) 
     itinerary = models.ForeignKey(Itinerary, null=True) 
     traveller = models.ForeignKey(User) 
     objects = ThreadManager() 

     def __str__(self): 
      return "Thread " + str(self.id) 

    class Message(models.Model): 
     message = models.TextField() 
     sender = models.ForeignKey(User, related_name='sent_messages') 
     thread = models.ForeignKey(Thread) 
     time_sent = models.DateTimeField(auto_now_add=True) 
     time_read = models.DateTimeField(null=True, blank=True) 

Models.py(tourlisting):

 
    from __future__ import unicode_literals 
    from django.db import models 
    from auth.models import UserProfile, User 
    from djmoney.models.fields import MoneyField 
    from django.utils import timezone 
    from django_countries.fields import CountryField 
    import datetime 

    class Category(models.Model): 
     name = models.CharField(max_length=100, unique=True) 
     def __str__(self): 
      return self.name 

    class Tour(models.Model): 
     tour_name = models.CharField(max_length=100, default='') 
     tour_desc = models.CharField('Tour Description', max_length=10000, default='') 
     tour_guide = models.ForeignKey(UserProfile) 
     tour_length = models.IntegerField('Tour length (days)', default = 3) 
     country = CountryField(default='', max_length=100) 
     city = models.CharField(max_length=1000, default='') 
     ppl_min = models.IntegerField('Minimum no. of travellers', default=1) 
     ppl_max = models.IntegerField('Maximum no. if travellers', default=10) 
     price = MoneyField(max_digits=6,decimal_places=2, default_currency='USD', default=50) 
     category = models.ForeignKey(Category) 
     pub_date = models.DateField('date published', auto_now_add=True) 
     active = models.BooleanField(default=True) 
     deleted = models.BooleanField(default=False) 
     def __str__(self): 
      return "{0} by {1}".format(self.tour_name, self.tour_guide) 
     def was_listed_recently(self): 
      return self.pub_date >= timezone.now() - datetime.timedelta(days=7) 

堆棧跟蹤:

 

    Environment: 


    Request Method: GET 
    Request URL: http://localhost:8000/admin/messaging/thread/2/change/ 

    Django Version: 1.9.2 
    Python Version: 2.7.10 
    Installed Applications: 
    ('django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.admin', 
    'django.contrib.staticfiles', 
    'messaging', 
    'crispy_forms', 
    'djmoney', 
    'django_countries', 
    'haystack', 
    'pysolr', 
    'django_tables2', 
    'phonenumber_field', 
    'django_extensions') 
    Installed Middleware: 
    ('django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware') 


    Template error: 
    In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33 
     ascii 23 : {% endblock %} 
     24 : {% endif %} 
     25 : 
     26 : {% block content %} 
     27 : {% block object-tools %} 
     28 : {% if change %}{% if not is_popup %} 
     29 : 
     30 :  {% block object-tools-items %} 
     31 :  
  • 32 : {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %} 33 : {% trans "History" %} 34 :
  • 35 : {% if has_absolute_url %}
  • {% trans "View on site" %}
  • {% endif %} 36 : {% endblock %} 37 : 38 : {% endif %}{% endif %} 39 : {% endblock %} 40 : {% csrf_token %}{% block form_top %}{% endblock %} 41 : 42 : {% if is_popup %}{% endif %} 43 : {% if to_field %}{% endif %} Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 174. response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 172. response = response.render() File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render 160. self.content = self.rendered_content File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content 137. content = template.render(context, self._request) File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py" in render 95. return self.template.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 206. return self._render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render 197. return self.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render 173. return compiled_parent._render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render 197. return self.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render 173. return compiled_parent._render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render 197. return self.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render 69. result = block.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render 69. result = block.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render 220. nodelist.append(node.render_annotated(context)) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render 209. return template.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 208. return self._render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render 197. return self.nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render 220. nodelist.append(node.render_annotated(context)) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render 220. nodelist.append(node.render_annotated(context)) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render 326. return nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render 326. return nodelist.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 992. bit = node.render_annotated(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated 959. return self.render(context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render 1049. return render_value_in_context(output, context) File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_value_in_context 1026. value = force_text(value) File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text 78. s = six.text_type(s) File "/usr/local/lib/python2.7/dist-packages/django/utils/html.py" in 381. klass.__unicode__ = lambda self: mark_safe(klass_unicode(self)) File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in __str__ 43. return self.as_widget() File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in as_widget 101. return force_text(widget.render(name, self.value(), attrs=attrs)) File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py" in render 291. 'widget': self.widget.render(name, value, *args, **kwargs), File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render 528. options = self.render_options(choices, [value]) File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render_options 554. for option_value, option_label in chain(self.choices, choices): File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __iter__ 1110. yield self.choice(obj) File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in choice 1117. return (self.field.prepare_value(obj), self.field.label_from_instance(obj)) File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in label_from_instance 1180. return smart_text(obj) File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in smart_text 42. return force_text(s, encoding, strings_only, errors) File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text 80. s = six.text_type(bytes(s), encoding, errors) Exception Type: UnicodeEncodeError at /admin/messaging/thread/2/change/ Exception Value: 'ascii' codec can't encode character u'\xc5' in position 35: ordinal not in range(128)
    +0

    你可以在你的模型中定義__unicode__函數。 – Pavan

    +0

    如果我理解正確,我無法從外部應用訪問模型。 –

    +0

    你可以添加整個回溯? – trinchet

    回答

    0

    添加到您的模型:

    @python_2_unicode_compatible 
    class Thread(models.Model): 
        ... 
    

    和更新這個類的STR方法:

    class Tour(models.Model): 
        ... 
        def __str__(self): 
         return "{0} by {1}".format(self.tour_name.encode('utf8'), self.tour_guide) 
    

    您還需要確保用戶配置的表示被編碼爲utf8

    +0

    不幸的是,沒有工作 –

    +1

    更新了答案 – trinchet

    +0

    嘗試並得到了一個稍微長一點的錯誤:''ascii'編解碼器無法解碼位置35中的字節0xc3:序號不在範圍內128)。你傳入了)' –

    0

    試試這個:(U「」從unicode字符串)我認爲這可能是問題

    def __str__(self): 
        return u"{0} by {1}".format(self.tour_name, self.tour_guide) 
    
    相關問題