2015-09-14 97 views
1

我想在django模板中简单显示模型formset。我收到以下错误显示formset时Django UnicodeEncodeError:ascii编解码器不能编码字符

enter image description here

enter image description here

这里是我试图显示:实际表单集形式

enter image description here

中在view.py,在这里是相关的代码片段:

# 
# create Address Model Form Set 
# 
AddressFormSet = modelformset_factory(Address, form=businessForms.AddressModelForm) 

if request.method == 'GET': 

    businessModelForm = businessForms.BusinessModelForm(instance = business) 

    addressModelFormSet = AddressFormSet(queryset=Address.objects.filter(business__id=business.id)) 
    #addressModelFormSet = AddressFormSet() 

    print addressModelFormSet.is_valid() /* prints False */ 
    print addressModelFormSet.errors  /* prints [] empty list */ 

    return render(request, "business_profile.html", { 'businessModelForm' : businessModelForm, 
                 'addressModelFormSet': addressModelFormSet }) 

我认为表单的有效性与这个错误无关,因为我们检查POST请求的有效性,但我可能是错的。尽管对于formset没有显示错误列表。

的AddressModelForm:

class AddressModelForm(ModelForm): 

    class Meta: 
     model = Address 
     fields = ['street_address', 'address_line2', 'city', 'state', 'zip_code'] 

模型定义:

class Country(models.Model): 
    country_name = models.CharField(max_length = 50) 
    country_code = models.CharField(max_length = 2) 
    phone_code = models.CharField(max_length = 3, default = '000') 
    country_name_ar = models.CharField(max_length = 50, default = '') 

    #many-to-many fields 
    currencies = models.ManyToManyField(Currency) 

    def __str__(self): 
     return "%s" % self.country_name 

class City(models.Model): 
    city_name = models.CharField(max_length = 93) 
    city_name_ar = models.CharField(max_length = 93, default = '') 
    country = models.ForeignKey(Country) 

    def __str__(self): 
     return ("%s" % self.city_name) + "," + str(self.country) 

class Address(models.Model): 
    street_address = models.CharField(max_length = 500) 
    address_line2 = models.CharField(max_length = 500, default = '') 
    city = models.ForeignKey(City) # country included implicitly in city 
    zip_code = models.CharField(max_length = 5, default = '') 
    state = models.CharField(max_length = 2, default = '') 

    def __str__(self): 
     usStr = ("%s" % self.street_address) + "," + str(self.city) + "," + self.state + "," + self.zip_code 
     nonUsStr = ("%s" % self.street_address) + "," + str(self.city) 

     if self.state != '': 
      return usStr 
     else: 
      return usStr 

我怀疑那个城市模型city_name_ar这是城市名阿拉伯语领域的事实...

更新 如果我从AddressModelForm中删除“城市”,或将该字段覆盖为CharField,我不会不会得到这个错误,但是,我得到的城市ID是没用的文本字段...

+0

我怀疑你的数据库没有正确地存储东西。 –

+0

@ IgnacioVazquez-Abrams请你解释一下吗?看到我上面的更新,当我从模型窗体中删除城市或重写字段以显示文本字段时,它工作正常 –

+0

打开数据库。检查表格模式。检查存储在该字段中的字节序列。 –

回答

0

您使用的是Python 2.x.在2.x中,模型应该要么有一个__unicode__方法,而不是或除了__str__方法,每种方法应返回合适的类型(Unicode进行__unicode__,编码字节__str__),或者如果你”你应该使用python_2_unicode_compatible装饰重新使用Django的最新版本。如果你打算在不久的将来继续使用2.x,我建议你只写__unicode__方法,而不用打扰装饰器,因为你串联了字符串表示,我不太确定它是怎么做的。

一些相关文档是:

https://docs.djangoproject.com/en/1.8/ref/utils/#django.utils.encoding.python_2_unicode_compatible

https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods

无论哪种方式,你应该避免不指定编码转换数据库值(这是通过周围为Unicode对象)。一般来说,最简单的方法只是定义返回unicode的,例如方法:

class Country(models.Model): 
    # as above, except 
    def __unicode__(self): 
     return self.country_name 

或者:

同样,对于您的其他车型:

class City(models.Model): 
    ... 
    def __unicode__(self): 
     return self.city_name + "," + unicode(self.country) 

或(未经测试,可能需要此处也请拨打unicode(self.country)):

@python_2_unicode_compatible 
class City(models.Model): 
    ... 
    def __str__(self): 
     return self.city_name + "," + str(self.country) 
+0

感谢您的回答,但这并没有解决它,现在python.exe崩溃,我得到访问冲突异常(看到在Visual Studio调试器)。我用了你给出的两个选项,结果相同 –

+0

这很奇怪。猜测中,您的数据库中存储的字节与表中声明的字符集不匹配。我很惊讶,这可能会产生你正在报告的错误,但它不可能远程调试。 –

相关问题